KEMBAR78
Java basics and java variables | PPTX
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

Java basics and java variables

  • 1.
  • 2.
    Variables A variable issomething 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 variablesin 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) publicclass 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 specialsymbols 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 isa 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 languageallows 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 roughlyequivalent 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 Thestatements 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; • Thecondition 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 somethinghere } else { //do something else here }
  • 14.
    The switch Statement Unlikeif-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 classSwitchDemo { 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 tutorialson Java visit programminghunk