SlideShare a Scribd company logo
2
Variables
A variable is something which can hold some
value, or this is like a container for holding some
value; and off course this value is always subject
to change. The name of variable is always
preceded with the name of data type in JAVA.
For example
int speed=0;
Data type Name of variable
Value of variable
Most read
3
Type of variables in JAVA
The Java programming language defines the
following kinds of variables:
• Instance Variables (Non-Static Fields)
• Class Variables (Static Fields)
• Local Variables
• Parameters
Most read
4
Example(Type of Variables)
public class VariableDemo
{
int var1=0; //Each Instance of this class will have
//its own copy of this variable
static int var2=6; //Only one(common) copy of class variable will be
// available with all instances of this class
public void showArea(float r)
{
float pi=3.144f; // Local variable have local scope to that particular
// method or code of block(example for loop;)
System.out.println(“Area of circle is = ”+ pi*r*r);
}
}
Instance variable/Field/ object Variable
Class/static variable
Parameters
Local variable
Most read
BASICS
Variables
A variable is something which can hold some
value, or this is like a container for holding some
value; and off course this value is always subject
to change. The name of variable is always
preceded with the name of data type in JAVA.
For example
int speed=0;
Data type Name of variable
Value of variable
Type of variables in JAVA
The Java programming language defines the
following kinds of variables:
• Instance Variables (Non-Static Fields)
• Class Variables (Static Fields)
• Local Variables
• Parameters
Example(Type of Variables)
public class VariableDemo
{
int var1=0; //Each Instance of this class will have
//its own copy of this variable
static int var2=6; //Only one(common) copy of class variable will be
// available with all instances of this class
public void showArea(float r)
{
float pi=3.144f; // Local variable have local scope to that particular
// method or code of block(example for loop;)
System.out.println(“Area of circle is = ”+ pi*r*r);
}
}
Instance variable/Field/ object Variable
Class/static variable
Parameters
Local variable
Operators
Operators are special symbols that perform
specific operations on one, two, or
three operands, and then return a result
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Expressions
An expression is a construct made up of
variables, operators, and method invocations,
which are constructed according to the syntax of
the language, that evaluates to a single value.
For example
int empId = 97312;
testArray[1] = 100;
System.out.println("Element 2at index 1: " +
testArray[0]);
Java programming language allows you to
construct compound expressions from various
smaller expressions
Like a*b*c;
However these can be ambiguous
Like a +b /100  Ambiguous, not
recommended
Or like (a+b)/100  Not Ambiguous,
recommended
Expressions must be balanced correctly using
parenthesis
Statements
Statements are roughly equivalent to sentences in
natural languages. A statement forms a complete
unit of execution. The following types of
expressions can be made into a statement by
terminating the expression with a semicolon (;).
• Assignment expressions // speed=107;
• Any use of ++ or -- //a++;
• Method invocations //c= obj.add(15,17);
• Object creation expressions //Cal obj=new Cal();
Blocks
if (condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
Control Flow Statements
The statements inside your source files are generally
executed from top to bottom, in the order that they
appear. Control flow statements, however, break up the
flow of execution by employing decision making, looping,
and branching, enabling our program
to conditionally execute particular blocks of code.
1. decision-making statements (if-then, if-then-
else, switch),
2. looping statements (for, while, do-while), 3.
3. branching statements (break, continue, return)
supported by the Java programming language.
if
• if(condition)statement;
• The condition is Boolean expression, its
outcome can be only true or false
• Relational operators like <,>,== can be used to
create condition
If-else
if (condition)
{
//do something here
} else
{
//do something else here
}
The switch Statement
Unlike if-then and if-then-else statements,
the switch statement can have a number of
possible execution paths. A switch works with
the byte, short, char, and int primitive data
types. It also works with enumerated types ,
the String class, and a few special classes that
wrap certain primitive
types: Character, Byte, Short, and Integer
Switch example
public class SwitchDemo {
public static void main(String[] args) {
int day = 3;
String day;
switch (month) {
case 1: day= “monday";
break;
case 2: day = “tuesday";
break;
case 3: day = “wednesday";
break;
case 4: day = “thrusday";
break;
case 5: day = “friday";
break;
default: day = “holiday";
break;
}
System.out.println(day);
}
}
For more tutorials on Java visit
programminghunk

More Related Content

What's hot (20)

Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Access modifier and inheritance
Access modifier and inheritanceAccess modifier and inheritance
Access modifier and inheritance
Dikshyanta Dhungana
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
JayasankarPR2
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
Java variable types
Java variable typesJava variable types
Java variable types
Soba Arjun
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
JayasankarPR2
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Java variable types
Java variable typesJava variable types
Java variable types
Soba Arjun
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 

Similar to Java basics and java variables (20)

java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
RajkumarHarishchandr1
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
Ashita Agrawal
 
Java_Roadmap.pptx
Java_Roadmap.pptxJava_Roadmap.pptx
Java_Roadmap.pptx
ssuser814cf2
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
Professional Guru
 
Java Programming
Java Programming Java Programming
Java Programming
RubaNagarajan
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
manaswinimysore
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
UNIT 2 programming in java_operators.pptx
UNIT 2 programming in java_operators.pptxUNIT 2 programming in java_operators.pptx
UNIT 2 programming in java_operators.pptx
jijinamt
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
Rakesh Madugula
 
JAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languaugeJAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languauge
lakshyajain0740
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
SaqlainYaqub1
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
Prerna Sharma
 
Mobile computing for Bsc Computer Science
Mobile computing for Bsc Computer ScienceMobile computing for Bsc Computer Science
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
Programming with Java - Essentials to program
Programming with Java - Essentials to programProgramming with Java - Essentials to program
Programming with Java - Essentials to program
leaderHilali1
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
Intelligo Technologies
 
CSL101_Ch1.ppt Computer Science
CSL101_Ch1.ppt          Computer ScienceCSL101_Ch1.ppt          Computer Science
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
UNIT 2 programming in java_operators.pptx
UNIT 2 programming in java_operators.pptxUNIT 2 programming in java_operators.pptx
UNIT 2 programming in java_operators.pptx
jijinamt
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 
JAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languaugeJAVA Basics Presentation for introduction to JAVA programming languauge
JAVA Basics Presentation for introduction to JAVA programming languauge
lakshyajain0740
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
SaqlainYaqub1
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
Prerna Sharma
 
Mobile computing for Bsc Computer Science
Mobile computing for Bsc Computer ScienceMobile computing for Bsc Computer Science
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
Programming with Java - Essentials to program
Programming with Java - Essentials to programProgramming with Java - Essentials to program
Programming with Java - Essentials to program
leaderHilali1
 
CSL101_Ch1.ppt Computer Science
CSL101_Ch1.ppt          Computer ScienceCSL101_Ch1.ppt          Computer Science
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Ad

Recently uploaded (20)

What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Revista digital preescolar en transformación
Revista digital preescolar en transformaciónRevista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke WarnerPublishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Revista digital preescolar en transformación
Revista digital preescolar en transformaciónRevista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke WarnerPublishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Ad

Java basics and java variables

  • 2. Variables A variable is something which can hold some value, or this is like a container for holding some value; and off course this value is always subject to change. The name of variable is always preceded with the name of data type in JAVA. For example int speed=0; Data type Name of variable Value of variable
  • 3. Type of variables in JAVA The Java programming language defines the following kinds of variables: • Instance Variables (Non-Static Fields) • Class Variables (Static Fields) • Local Variables • Parameters
  • 4. Example(Type of Variables) public class VariableDemo { int var1=0; //Each Instance of this class will have //its own copy of this variable static int var2=6; //Only one(common) copy of class variable will be // available with all instances of this class public void showArea(float r) { float pi=3.144f; // Local variable have local scope to that particular // method or code of block(example for loop;) System.out.println(“Area of circle is = ”+ pi*r*r); } } Instance variable/Field/ object Variable Class/static variable Parameters Local variable
  • 5. Operators Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result
  • 6. Operators Precedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
  • 7. Expressions An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. For example int empId = 97312; testArray[1] = 100; System.out.println("Element 2at index 1: " + testArray[0]);
  • 8. Java programming language allows you to construct compound expressions from various smaller expressions Like a*b*c; However these can be ambiguous Like a +b /100  Ambiguous, not recommended Or like (a+b)/100  Not Ambiguous, recommended Expressions must be balanced correctly using parenthesis
  • 9. Statements Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;). • Assignment expressions // speed=107; • Any use of ++ or -- //a++; • Method invocations //c= obj.add(15,17); • Object creation expressions //Cal obj=new Cal();
  • 10. Blocks if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one
  • 11. Control Flow Statements The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling our program to conditionally execute particular blocks of code. 1. decision-making statements (if-then, if-then- else, switch), 2. looping statements (for, while, do-while), 3. 3. branching statements (break, continue, return) supported by the Java programming language.
  • 12. if • if(condition)statement; • The condition is Boolean expression, its outcome can be only true or false • Relational operators like <,>,== can be used to create condition
  • 13. If-else if (condition) { //do something here } else { //do something else here }
  • 14. The switch Statement Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types , the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer
  • 15. Switch example public class SwitchDemo { public static void main(String[] args) { int day = 3; String day; switch (month) { case 1: day= “monday"; break; case 2: day = “tuesday"; break; case 3: day = “wednesday"; break; case 4: day = “thrusday"; break; case 5: day = “friday"; break; default: day = “holiday"; break; } System.out.println(day); } }
  • 16. For more tutorials on Java visit programminghunk