4. Values and Data Types (1)
4. Values and Data Types (1)
A 65
- -
- -
Z 90
a 97
- -
- -
z 122
Blank space 32
Java Escape Sequences
ESCAPE SEQUENCE MEANING
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
int a; a= 5;
long b; b= 548745;
float f; f= 3.25;
double d; d= 2.25478;
boolean p; p= false;
4) punctuators/ Separators
SYMBOL NAME DESCRIPTION
1) () parentheses Used in:
-Method signatures.
-Loops
-Conditional statements
2) {} braces Used in:
-Declaration of types.
-Block of statements
-Array initialization
3) [] brackets Used to:
- Array declaration
4) ; Semicolon Used to:
-Terminate statements
-Separate the initialization
code
SYMBOL NAME DESCRIPTION
5) : colon Used to:
-Define loops.
-Define switch
statements.
6) , comma Used to:
-Separate values
-Separate variables.
7) . period Used to:
-Separate package
names.
-Separate fields or
methods.
5) operators
- Operators are special symbols that perform
specific operations on one, two or more
operands.
- Operands can be constant or a variable.
- Hence, an operation is performed using at
least one symbol and at least one value.
- The symbol used in an operation is called an
operator.
- A value involved in an operation is called an
operand.
Types of operators
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Conditional/Ternary operators
I) Arithmetic operators
&& (and) Final result is True only if all (A>B && A>C) is False
conditions are True. It is False if any
one condition is False. (A>B && A==C) is True
|| (or) Final result is True if any one (A>B && A>C) is True
condition is True. It is False only when
all conditions are False. (A<B && A>C) is False
1) Increment operator: ++
2) Decrement operator: --
Ex: If x=10
x++; gives 11
x--; gives 9
VI) Conditional/Ternary operator
exp1 ? exp2 : exp3
Evaluate if Evaluate if
True/ False true false
a=(x > y) ? x : y
will assign the value 20 to ‘a’
a=(x < y) ? x : y
will assign the value 10 to ‘a’
VARIABLES
- A variable is a container, that stores a data value
in it.
- It is a basic unit of storage in a java program.
- All variables must be declared before they can
be used.
Variable Definition in Java
Syntax:
type variable_name;
type variable_name, variable_name,
variable_name;
Example
/* variable definition and initialization */
int width, height=5;
char letter='C';
float age, area;
double d;
/* actual initialization */
width = 10;
age = 26.5;
Dynamic Initialization of a variable
j=5
K=5
Multiple Assignments
public class demo
{
public static void main(String args[])
{
int j,k;
k=j=10;
S.O.P(“j=”+j);
S.O.P(“k=”+k);
}
}
2) Arithmetic operators
Public class Arithmetic
{
public static void main(String args[])
{
int x, y=10, z=5;
x=y+z;
System.out.println(“the ans is”+x);
x=y-z;
System.out.println(“the ans is”+x);
x=y/z;
System.out.println(“the ans is”+x);
x=y%z;
System.out.println(“the ans is”+x);
}
}
Output:
x=15
x=5
x=2
x=0
3) INCREMENT/DECREMENT OPERATORS
displays 2.
int count=1;
System.out.println(count++);
displays 1.
Example 5
class PrePostDemo
{
public static void main(String args[])
{
int i = 3;
i++;
System.out.println(i); // "4" : use then change
++i;
System.out.println(i); // "5" :change then use
System.out.println(++i); // "6" : change then use
System.out.println(i++); // "6" :use then change
System.out.println(i); // "7“ : change from previous
}
}
4) RELATIONAL OPERATOR
class RelDemo
{
public static void main(String args[])
{
int a = 10, b = 15, c = 15;
System.out.println(" a > b = " + (a > b));
System.out.println(" a < b = " + (a < b));
System.out.println(" b >= a = " + (b >= a));
System.out.println(" b <= a = " + (b <= a));
System.out.println(" b == c = " + (b == c));
System.out.println(" b != c = " + (b != c));
}
}
5) LOGICAL OPERATORS
}
6) Conditional/ternary
public class TernaryEx
{
public static void main(String[] args)
{
int minVal, a=3, b=2;
minVal = a < b ? a : b;
System.out.println("min = " + minVal);
}
}