Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Java Datatype, Variables
& Operators Data Types in Java Data Types
Primitive (Standard) Non Primitive
(Derived)
Numeric Non Numeric Array Interface
Class
Integer Floating Point char boolean
float double
byte short int long
Data Types in Java
Data Types Size Description Range Default Value
byte 8 Smallest integer type -128 to 127 (or 0 -27 to 27-1) short 16 short integer -215 to 215-1 0
int 32 Most commonly used integer -231 to 231-1 0
type long 64 Long integer -263 to 263-1 0L
float 32 Single precision floating point 3.4E-038 to 0.0f
no. 3.4E+038 double 64 Double precision floating point 1.7E-308 to 0.0d no. 1.7E+308
char 16 A single character Total no of ‘\u0000’
characters 0 to 65535` boolean 8 A boolean value true/false false Java Tokens Tokens are smallest individual units in a program. Each token represents a sequence of characters that can be treated as a single logical entity. Reserved word, identifier, constant, operator & punctuation symbols are typical tokens. Octal Integer:-Octal Integer in java are denoted by a leading zero(0). For e.g, 037, 0465, 04321 etc. Hexa Decimal Integer:- Hexa Decimal Integer in java is represented by a leading ox or OX. For e.g, OX82, ox5A, OXFF etc. Floating Point:- In scientific or exponent form, the floating point no+ a suffix specifies a power of 10 by which the no is to be multiplied. The exponent can be represented by E or e. It is followed by a decimal no which can be +ve / -ve. For e.g, 6.02337E23, 341E0-7, 0.173e+123 General Form:- Mantissa E Exponent 1.2E102=1.2*10102 ( Mantissa part can be either in decimal or fractional but exponent form must always be in decimal whole no). Operators • Arithmetic(+,-,*,/,% etc) • Assignment(=) • Comparison(==) • Unary (++, -- etc) • Shift(<<, >>, >> etc) • Bitwise( &, |, ~, ^ etc) • Logical (&&, ||, !) • Conditional(<,<=,>,>=,!= etc) • Instance of & Member Selection • new Operators a%b=a-(a/b)*b -17%5=-2 -17%-5=-2 17%-5=2 17%5=2 a=12.5f , b=3.0f =>a%b=0.5 Assignment Operator:- It can be simple assignment(=) or short hand operator(+=,-=,/=,%= etc) Shift Operator:- It allows to perform bit manipulation on data. Shift operators are not applicable for floating point datatype. Operator General Form Operation
>> op1>>op2 Shift bits at op1 right by distance of op2.
<< op1<<op2 Shift bits at op1 left by distance of op2.
>>> (zero fill right shift/ op1>>>op2 Shift bits at op1 right by distance of op2 unsigned shift operator (unsigned). 0’s are placed in the MSB. Operators • +ve no right shift:- 12>>2 12:- 0 0 0 0 0 1 1 0 0 12>>2=12/2^2=3 3:- 0 0 0 0 0 0 0 1 1
10<<2 10:- 0 0 0 0 0 1 0 1 0 10>>2=10*2^2=2 2:- 0 0 0 0 0 0 0 1 0 Operators Short Circuit Operator:- && and || operators are called short circuit operators. Here, the 1st expression is evaluated & then we found the result, unless the 2nd expression is not evaluated, we get the result. • (condn1)&&(condn2)-> If condition1 false then the result will always false. • (condn1)||(condn2)-> If condition1 true then the result will always true. Instanceof Operator:- This operator tests whether an instance derive from a particular class or interface. The return type is boolean. General form-> object instanceof class; It is also called runtime operator. For e.g, “rose instanceof flower” is true if the object rose belongs to the class flower otherwise it is false. Operator Precedence Rank Operator Type Operators 1 Function call, dot & array reference (), [] 2 Unary ++, --, -, ~, ! 3 Creation, type casting new, (type) expression 4 Multiplicative *, /, % 5 Additive +, - 6 Shift >>, <<, >>> 7 Relation inequality >, >=, <, <=, instanceof 8 Relation equality ==, != 9 Bitwise And & 10 Bitwise XOR ^ 11 Bitwise OR | 12 Logical And && 13 Logical OR || 14 Ternary (Conditional) ?: 15 Assignment & Shorthand Assignment =, +=,*=,-=, /=, %=, &=, ^=, !=, <<=, >>=, >>>= Operator Precedence In Java all binary operators except for the assignment operators are evaluated from left to right order. Assignment operators are evaluated from right to left. Unary & ternary operators are also evaluated from right to left. Thank You