Professional Documents
Culture Documents
Core Java
Core Java
BY-A.K.JAISWAL
Introduction
Java is Simple high Level ,Platform independent , Architecture Neutral, Secure, Robust ,Multithreaded,
Distributed, and Object Oriented programming language.
Simple
Java is easy to learn because most of the complex or confusing feature of c and C++ like pointers ,
operator overloading etc are not provided in java.
Secure
Java programs run within the JVM which protects from unauthorized or illegal access to system
resources.
Platform independent/pointer
Java program can be executed on any kind of machine containing any cpu or any operating
system.
Architecture neutral
Since java application can run on any kind of CPU so it is architecture neutral.
Robust
Java is robust because of following..
Strong memory management
No pointer
Exception handling
Type checking mechanism
Platform independent
Multithreaded
Java supports multithreaded programming which allows us to write programs that do many things
simultaneously
Distributed
Using RMI and EJB we can create distributed application in java
Object oriented
Java follows object oriented programming model that helps to break the complex code into easy
to understand objects
Programming Language
Language
It is the medium of communication .
program
It is a set of instruction for a device to perform to some specific task.
Programming Language
It contain set of grammatical rules or syntax to write a program for instructing machine to
perform specific tasks.
Binary digits (0,1) will be used to write the instruction in Low Level Language
Instruction write in binary language or native language will be understand by the machine directly .
No transfer program is required.
PROBLAM
It is very complex to write ,modify and debug the instruction written in Binary Language .
Binary code or Native code is platform dependent.
Assembly codes like ADD ,SUB,MUL,DIV,MOV etc .Will be used to write the instruction in Middle Level
Language .
Instruction written in assembly language will not be understand by the machine directly .
Assembler is a program that should be used to convert Assembly Language To Machine Language.
PROBLAM
Human understand codes like English words will be used to write the instruction in High Level
Language .
Instructions written in High Level Language will not be understood by the machine directly .
Computer or interpreter is a program that should be used to converter High Level Language to
Machine Language.
Advantage
it is easy to write ,modify and the instruction written using High Level Language.
Hello.c
C Source Code
C Compiler
Will be executed
Successfully
WINDOWS
Will not be
executed
Successfully
MAC OS
Will not be
executed
Successfully
LINUX
Hello.java
java Source Code
Java Compiler
Hello.class
JVM
Native Code
Will be
executed
Successfully
WINDOWS
Byte Code
JVM
Native Code
Will be
executed
Successfully
MAC OS
JVM
Native Code
Will be
executed
Successfully
LINUX
Human understandable code written using High Level Programming language is called as Source code.
JIT compiler compiles or translates or converts the necessary part of the byte code into machine code instead
of converting line by line . Because of this performance of java program has improved.
Development Kit)?
It is a set of various programs which are required for developing and executed the java programs.
It is a platform dependent. Various JDKs are provided for various Operating system.
Following are various utility programs provided under JDK.
Javac
Java
Javap
Jar
Etc
Source File
JRE
Etc
What is JRE ?
JRE stands for java runtime Environment . It is an implementation of JVM . It contain class
Libraries, interpreter, JIT Compiler etc. Only JRE enough to run the java program.
Development Tools
Hello.java
Javac
java
javap
jar
etc
Hello.class
Class Loader
Class Files
Method
Heap
Area
Memory
Execution
Engine
Stack
PC
Memory
Register
JIT
GC
Compiler
0101010
0101001
Operation System/Platform
Hardware
Other
Components
History
Java was designed by sun Microsystems in the early 1990s to solve the problem of
connection many household machines together.
Initial version of java is called as OAK.
Later java become popular for developing internet applications .
Java is developed by James Gosling and team.
James Gosling is considered as FATHER OF JAVA.
It was publicly released on 1995.
Now sun Microsystems is the part of Oracle Crop.
Editions
Enterprise Edition(EE)
Micro Edition(ME)
Code Name
Oak
Playground
Kestrel
Merlin
Released Year
1995
1996
1997
1998
2000
2002
J2SE 5.0
J2SE 6
JAVA SE 7
JAVA SE 8
Tiger
Mustang
Dolphin
2004
2006
2011
18 march 2014
Released Year
1998
J2EE1.3
2001
J2EE1.4
2003
JEE 5
2006
JEE 6
2009
JEE 7
2013
Technologies Added
JDBC 2.0
JNDI 1.2
Servlets 2.2
Jsp1.1
EJB 1.1
Java Mail API1.1
Servlets 2.3
JSP1.2
EJB2.0
Java Mail API 1.2
Servlets 2.4
JSP 2.0
EJB 2.1
Java Mail API1.3
Servlets 2.5
JSP 2.1
JSF 1.2
EJB3.0
JPA1.0
Java Mail API1.4
JDBC 4.0
Servlets 3.0
JSP 2.2
JSF 2.0
EJB 3.1
JPA2.0
JDBC 4.1
Servlets 3.1
JSP 2.3
JSF 2.2
EJB 3.2
JPA2.1
Installation step
Directory Structure
After installing JDK you will get the following directory structure.
JRE Derectory
JDK Directory
C:
E:
Program Files
Jdk 1.7.0
bin
Javac
Java.exe
Javap
lib
,, jar.exe
,,etc
jre
Srz.zip
Private
Jre
java
Jre7
Public
jre
Steps
Workspace
D:
E:
Jdk 1.7.0
bin
Java.exe,java.exe
Mukesh
Ankush
Hello.java
Java.exe,javap.exe
Jar.exe,,etc
If you set the path at command line then that path will be accessed as long as that
command prompt running .Even that path will not be accessed with other prompt
is running at a time. So it is temporary.
You can set the PATH permanently in System Environment Variables as follows.
Right click on My Computer and Select Properties.
Select Advanced Tab.
Click on Environment Variables Button.
Click on new Button in the User Variable
Provide the required info.
Variable the required info. PATH
Variable value
E:\jdk1.7.0\bin
Click on button in all the windows.
COMMENTS
Single Line Comments (//)
// the whole line will be ignored by the compiler.
Documents Comments
/** will be used to
*define documentation
*of any variable, method
*in java Source File.
*/
Multiple Line Comments(/* */)
/* you can define as
Many lines as
You want to
Ignore from compilation.
*/
C++
C++ support pointer
C++ supports multiple
inheritance with classes.
C++ supports Global Variables.
Java
Language Ref
Guide
Part 1
1. Character sets
Alphabets
Digits
Special Characters
White Space character
A-Z/a-z.
0-9
+-*&! @#$%^ etc
newline (\n), carriage return (\r)
etc
2. Keywords/Reserved words
Keywords are simple English words which are having predefined meaning in java
programming Language.
const & goto are the keywords but no implementation available. You cant use
these keywords in java program.
List of Keywords
Data types(8)
Class & object(9)
Access Modifier(3)
Modifier(9)
Package(2)
Control Statements(12)
Exception Handling(6)
Other data Type(1)
Valid Identifiers
Invalid Identifiers
Hello
Int
getClassName
Total_FFE
1stClass
true
student number
int
4. Data types
Data type represents
Type of data you want to use
Amount of memory allocation required for your data.
There are two types of data types.
Primitive data types
User defined data types.
byte
char
short
int
long
float
double
boolean
Boolean Types
byte
short
Integral Types
Primitive
Types
int
long
Numeric
Types
Floating
Point Types
char
float
double
Type
Size
Byte
N/D
Default
value
Min value
Max value
True or
-27(128)
0
false
27-1(127)
216-1 (65535)
-215
(32,768)
-231
(2147483648)
-263
1.4E-45
4.9E-324
Reference of
215-1 (32,767)
boolean
Byte
Char
1
2
Bits
N/D
8
16
short
16
false
0
ASCII-0
Unicode\u0000
0
Int
32
Long
Float
Double
Any
reference
type
8
4
8
8
64
32
64
64
0
0.0
0.0
Null
231-1
(2147483647)
263-1
3.40E38
1.79E308
The
corresponding
Type object
5. Variable
Variable is the container which holds user data.
Memory will be allocated for the variable while executing the program.
Value of the variable can be changed any number of times during the program
execution.
Syntax
<Data type> <varName>;
<Data type> <varName> =<value>;
EX
Int a;
String str;
str
4 bytes
null
8 bytes
Int a=99;
a
99
4 bytes
32597
SRI
8 bytes
32597
Types of Varialves
There are two types of variable based on data type used to declare the variable.
Primitive Variables
Reference Variables
Primtive Variables
EX
Int a;
Int b;
Double d1;
Double d2=9.9;
Reference Variables
Variable declared with user defined data types are called ass reference
variables.
EX
String str1;
String str2=Ankush ;
Primitive Variable
Reference Variable
Types of variables
There are three types of variables based on the scope of the variables.
Instance Variables.
Static Variables.
Local Variables.
Instance Variables
Variables declared in the class without using static keywords are called as instance
Variables.
Static Variables
Variables declared in the class using static keyword are called as Static Varaibles
Local Variables
Variables declared in the member of the class like method etc are called as Local
variables.
Class Hello {
Int a;
Static int b;
instance variable
static variable
void show(){
Int c;
local variable
}
}
Syntax
[modifiers]<data type> <varName>;
EX
byte b;
int a;
double d;
char ch;
String str;
Hello h;
Etc
EX
b=24;
ab=98765;
d=1234.34;
ch=A ;
str=Ankush
h=new Hello();
EX
byte b1,b2,b3,b4;
int a,b,c;
char ch1,cj2,ch3;
String str1.str2,str3;
Syntax
<var1>=<var2>=<var3>=<value/expression>;
EX
a=b=c=9898;
str1=str1=str3=Ankush ;
EX
Int a=120, b=54,c=98;
String str1=Ankush,str2=Mukesh ;
boolean b1=true,boolean b2=false;
6. Constant
Constant is a special variable whose value cant be modified during the program
execution.
Constant is also called as final variable.
Syntax
[modifier]final <data Type> <varName> = <value>;
EX
Final int a=99;
<varName>=<value> ;
EX
final int a;
a=98;
If the variable is declared as final and initialized in the same statement then
in the class final compiler will replace that variable with actual value.
SUMMARY
Default value of char is either ASCII-0 or UNICODE\u0000.
Default value of char is not space.
JVM will not provide default value for Local Variable.
Local variable must be initialized before using.
The local variable declared but not initialized and not used then that variable
declaration statement will be removed by compiler while translation to byte code.
JVM will provide default value for static Variable
Static variable can be accessed from Static method(main method)
JVM will provide default value for instance Variable
Instance variable cannot be accessed directly from Static method .
You cant declare multiple variables of different data types in single variable
declaration statement
You can declare multiple variables of same data types in a single statement
You can assign same value of multiple variables in a single statement
You can declare and initialize multiple variables of same data type in a single
statement
We cant declare two
Value of the variable can be changed any number of times during program
execution
Value of the final variable can not be changed
We cant use const keyword to declare constant
String str1=Ankush,str2=Mukesh ;
Int a,b;
Int c=a=b=99;
Declares a new variable c
Assigns value 99 to variable b
Assign value of variable b to variable a
Assigns value of variable a to newly declared variable c
int a=a=99;
declares variable a
assign value 99 to variable a
Assign value of variable a to variable a
Int a=b,b=99;//invalid
Declares variable a
Trying to assign value of variable b to variable a
7 .Literals
Data type
Int
Data type
String
Variable Name
a=
Literals
123;
Variable Name
str =
Ankush ;
Literals
Literals
Non Numeric
Numeric
boolean
Signed
Unsigned
char
integral
Floating
Types of Literals
Boolean Literals
Character Literals
String Literals
Integral Literals
Floating Literals
null Literals
Boolean Literals
boolean Literals can be assigned to a boolean type variables
there are two boolean literals
true
false
Character Literals
String
Escape sequence
ESCAPE SEQUENCE is a special notion is used to represent some special
characters which cant be represented as it is.
Escape Sequence
Description
Tab space
\t
\b
Back space
\n
New line
\r
Carriage return
\f
Formfeed
\
Single quote character
\
Double quote character
\\
Backslash character
ASCII Character sets
ASCII stands for American Standard Code for information Interchange.
Every character enclosed in single quotation marks will have an integer equivalent
value called as ASCII value.
UNICODE Range
\u0000 (0) to \uFFFF(65535)
Char
ASCII
0
1
2
3
4
5
6
7
8
9
:
;
48
49
50
51
52
53
54
55
56
57
58
59
255
\0
\377
OCTAL
060
061
062
063
064
065
066
067
070
071
072
073
UNIC
30
31
32
33
34
35
36
37
38
39
3a
3b
Char
ASCII
<
=
>
?
@
A
B
C
D
E
F
G
60
61
62
63
64
65
66
67
68
69
70
71
OCTAL
074
075
076
077
100
101
102
103
104
105
106
107
UNIC
3c
3d
3e
3f
40
41
42
43
44
45
46
47
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
a
b
c
d
e
f
g
h
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
97
98
99
100
101
102
103
104
110
111
112
113
114
115
116
117
120
121
122
123
124
125
126
127
130
131
132
141
142
143
144
145
146
147
150
48
49
4a
4b
4c
4d
4e
4f
50
51
52
53
54
55
56
57
58
59
5a
61
62
63
64
65
66
67
68
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
151
152
153
154
155
156
157
160
161
162
163
164
165
166
167
170
171
172
69
6a
6b
6c
6d
6e
6f
70
71
72
73
74
75
76
77
78
79
7a
String Literals
String literals is a collection of zero or more characters enclosed between
double quotation marks.
Integer Literals
Integer Literals can be assigned to one of the integral data type byte, short , int,
long
Integer Literal size is depending on integral data type you are using
Default type of integer Literal is int
There are four types of integer Literals
Decimal literals
Octal Literals
Hexadecimal Literals
Binary Literals(from java 7)
Decimal Literals
12345
98875437
Octal Literals
Octal Literal is a valid number from octal number system
The base or radix of octal number system is 8 digits ranging from 0 to 7 are allowed
to from the octal literals
Octal Literals must start with 0(zero)
hexadecimal Literals
Hexadecimal Literal is a valid number from hexadecimal number system
The base or radix of hexadecimal number system is 16 i.e only 16 digits
ranging from 0 to 9
And A/a to F/f are allowed to from the hexadecimal Literals
Hexadecimal Literals must start with 0X/0x.
1/10
1/100
1/1000
12.876
10x Bigger
10x smaller
12345.67e-2 ->12345.67*102
Double x =0xap0;
a->10
//10*20=10.0
Double y =0xfp2D;
f->15
//15*22=60.0
float z =0xf.ap1F;
f->15
a->10
//(15*160+10/161)*21
//(15*1+.625)*2
//15.625*2)=31.25f
Double d1=0x1f.a5p1;
//(1*161+15*160+10/161+5/162)*21
//(16+15+10/16+15/256)*21
//(16+15+.625+.01953125)*2
//(31.64453125)*2
//63.2890625
null Literals
null is a value
it is default value for any reference variable
if the value of reference variable is null then it indicates that address/reference is
not available in the variable
to represent the unit of the values you can use comma (,) symbol in number representation
EX
2,45,12,452
you can do the same task with UNDERSCORE (_) from java 7 towards
EX
2_45_12_452
SUMMARY
4)
5)
6)
7)
8)
9) There are some character like backslash, single quote which cant be placed
in single quotes as it is. those special characters have to be represented as
escape sequence
10) Every character enclosed in single quotation marks will have an integer
equivalent value called as ASCII value
11) Char type variable can hold integer value ranging form 0-65535 only
12) Char type variable can hold UNICODE value ranging from \u0000-\uFFFF
only
13) Char type variable can hold OCTAL value ranging from \0-\377 only
14) When you print he char type variable which holds integer, UNICOE or
octal value then it displays corresponding character representation
15) If integer , UNICODE value dont have character representation then it
displays? Which indicates no character defined for that value.
16) When you assign character literals to int type variable then internally
ASCII value of that character will be assigned.
17) Source code can also be written in UNICODE representation
Int a=\u0037;
int a=7;
//valid
Int a=\u0039;
int a=7;
//valid
Int a=\u0037\u0037;
int a=79;
//valid
Int a=\u0037\u0037
int a=79;
//invalid
18) Empty string Literal is allowed whereas empty character literal is not
allowed
19) If reference variable of String type contains null value then it is called as
NULL String
20) You cant refer any member with NULL String . if you do so it results in
NullPointerException
21) If reference variable of String type contains zero character in double
quotations marks then it is called as EMPTY String .
22) When java compiler encounters any UNICODE/Octal value in String
literals then that UNICODE/Octal value will be replaced with actual
character
23) You can use escape sequence to represent UNICODE/Octal value as it is
in String literals
24) There are four types of integer literals
Decimal Literals
Octal V Literals
Hexadecimal Literals
Binary Literals(from java 7)
25) Default type if integer Literal is int
26) Integer value ranging from -2147483648 to 2147483647 can be
represented directly
27) If you want to represent the integer value out of int range then you need
to use L/l ssuffix
28) Decimal literals must not start with 0
29) Octal Literal must start with 0(zero)
30) Hexadecimal literal must start with 0X/0x
31) Binary literal must start with 0B/0b
32) When java compiler encounters octal , Binary or Hexadecimal Literals
then that will be replaced with decimal equivalent value
33) Default type of Floating point Literal is doule
34) You can use D/d as a suffix for the double value optionally
35) You must use F/f as a suffix for float value
36) When integer value starts with 0 then it will be considered as octal
literals
37) When floating point value starts with 0 then it will be considered as
floating point literals only , not as octal literals
38) When integer value starts with 0X/0x then it will be considered as
Hexadecimal literals
39) When floating point value starts with 0X/0x then it must use p in
fractional part
40) When integer value starts with 0B/0b then it will be considered as binary
literals
41) Floating point cannot be represented as Binary Literal
42) You can use UNDERSCORE in numeric Literals to represent unit of values
from java 7
43) When UNDERSCORE is used before first digit then it will be considered as
a variable
44) You cant use UNDERSCORE after the last digit
45) In the case of octal literal UNDERSCORE can be used just after 0
46) In the case of binary literal UNDERSCORE cant be used just after the
0B/0b
47) In the case Hexadecimal literal UNDERSCORE cant used just after 0X/0x
48) You can use more than one UNDERSCORE side by side between two
digits
49) You can use UNDERSCORE with floating point literal but not allowed just
before or after the decimal point
Part -2
Operator
Operator at used to perform operations by using operands
There are three types of operator depending on the number of operands required
Unary operator only one operand is required.
Binary operator-two operands are required
Ternary operator- three operands are required
Arithmetic operator
Arithmetic operator are used to form arithmetic expression to perform the
arithmetic operations.
Operands used in arithmetic expression can be of numeric type or char type
Result of arithmetic type operation is always int or wider than int type.
Operand1
operator
10
operand2
20
Name
Minus
plus
description
Return same value
with same sign
Return same value with
opposite sign
Description
Addition(sum)
Subtraction(difference)
Multiplication(product)
Division(quotient)
Modulus(remainder)
When the operands are int or lower than int type then result will be
int type only.
When any one operand or both the operands are wider than type
then result will be wider type.
2nd operand
Result type
byte
char
short
int
int
byte
float
float
char
int
char
short
short
etc
byte
char
byte
int
long
double
int
long
double
double
int
char
float
int
int
int
int
Long
double
float
float
double
double
int
int
float
It is a binary operator
+ operator can be used for two purposes
Arithmetic addition
String concatenation
Arithmetic Addition
When both the operands of + operator are numeric type
then it will perform arithmetic addition
EX
90 + 9 = 99;
Result of arithmetic addition is numeric type
String concatenation
When any one or both the operands of + operator are String type it will
perform String concatenation.
EX
90 + 9 = 909
9 + 90 = 909
90 + 9 =909
Result of String concatenation is String type.
byte
short
int
char
long
float
double
Type Casting
<Operand1> = <operand2>
Here operand2 is the source , operand1 is destination and we are assigning source
to destination.
Usually source type must be same as destination type
When source type is not same as destination type then source type must be
converted to destination type . This process of converting one type to another is
called as Type casting.
Type casting can be done in two ways.
Implicit casting
Explicit casting
Implicit casting
When type casting is happening automatically by the JVM then it is called as
Implicit Casting.
Explicit casting
When type casting is done by the programmer explicitly then it is called as
Explicit Casting.
SYNTAX
<dest Type><dest var> = (<dest type>)<source value>
EX
Int a=10;
byte b= (byte)a;
EX
byte b=10;
int a= b;
here you are casting byte to int . byte has a range from -128 to 127 where as int has a
range from -2147483648 to 2147483647 . so any value that fits in a byte will fit in a int
also . because of this there is no data loss and conversion is considered as sage.
Narrowing
Narrowing is the process of converting higher type to lower type
EX
Int a=300;
byte b=a;
byte b=(byte)a;
here you are casting int to byte . Range of byte is much smaller than range of int . what
happens when you convert an int value 300 to a byte.
This result 44 i.e data is lost so conversion is considered as unsafe.
Int a=300;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0
byte b1=(byte)a;
0 0 1 0 1 1 0 0
Value is
0+0+32+0+8+4+0+0=44
double val 300.9898;
byte b2=(byte)val;
=44.
=> a = a+9;
Operator
+=
-=
*=
/=
%=
&=
|=
description
Add right operand to left operand and
assign the result to left operand.
Subtract right operand from left operand
and assign the result to left operand
multiple right operand to left operand and
assign the result to left operand
divide right operand to left operand and
assign the result to left operand
Calculate modulus using two operands
and assign the result to left operand
assigns the result of the logical AND
assigns the result of the logical OR
^=
<<=
>>=
>>>=
Relational operators
These are binary operators
Relation operators forms the relational expressions
Result of relation expression is Boolean type i.e true or false
Comparison Operators
>
<
>=
<=
Greater than
Less than
Greater than or equal to
Less than or equal to
Operands of comparison operators must be number or char type
Equality operators
==
!=
description
Description
Equal to
Not equal to
Operands of equality operators can be numeric , char, boolean or reference type.
Logical operators
Logical operators are used to form logical expressions
The operands of these operators will be boolean type.
Description
Logical NOT
Logical AND
Logical OR
It is unary operator
It is used to inverse the result
OPERAND
true
false
Logical OR and Logical AND
Result
false
true
OP2
true
false
true
false
Result of Logical OR is false if both the operands are false otherwise true.
OP1
true
true
false
false
OP2
true
false
true
false
New operator
new operator is used to create the new object for class
it returns of the address of the address of newly created object
Instanceof Operator
it is used to check whether the given object belongs to specified class or not
Result of instanceof operator is Boolean value
It is also called as TYPE COMPARISION OPERATOR
Conditional operator
It is ternary operator
<Operand1> ? <Operand > : <Operand3>
Operand1 must be of boolean type
If operand1 is true then Operand2 will be returned otherwise Opetand3 will be
returned
Bitwise Operators
These operators will be used to perform the operation on individual bits of the
value.
Bitwise Operators
Description
Bitwise NOT
&
Bitwise AND
|
Bitwise OR
^
Exclusive OR (XOR)
<<
Left Shift
>>
Right shift
>>>
Unsigned Right Shift
Operands for the following Bitwise Operators can be of boolean type or integer
type .
Bitwise AND
Bitwise OR
Exclusive OR (XOR)
Operands for the following Bitwise Operands will be of Integer type only
Bitwise NOT
Left Shift
Right shift
Unsigned Right Shift
1s Complement
1s complement if a binary number is formed by changing 1s to 0s and 0s to 1s
0
Actual Bits
1s Complement
2s Complement
2s complement of a binary number is formed by adding 1 to the least significant bit
of 1s complement
1
+
1
0
1
1
Actual Bits
1s Complement
Adding 1 with value
2s Complement
Sign bit
64 32 16 8
0 0 0 0 1
4
1
2
1
1
1
Binary of 15
1s Complement (~x)=-16
64 32 16 8
0 0 1 0 1
4
1
2
0
1
1
Binary of 45
Int x =45;
Sign bit
1
1 0 1
Shortcut method
~OPERAND = -(OPERAND + 1)
EX
~198 => -(198 + 1)=> -199
~-198 => -(-198 +1) => -(-197) => 197
Bitwise operator
Bitwise AND operator is similar to Logical AND operator with following differences
Logical AND
Bitwise AND
OP1
OP2
1
1
0
0
1
0
1
0
1
0
0
0
Bitwise OR Operator
Bitwise OR operator is similar to logical OR Operator with following defferences.
Logical OR
Operands are Boolean type only
Bitwise OR
Operands can be Boolean as well as integral
type
When first expression value is true then
second expression also will be evaluated
When operands are Boolean type then
result is boolean type
When operands are integral type then
result is integral type.
OP1
OP2
1
1
0
0
1
0
1
0
1
0
1
0
OP1
OP2
1
1
0
0
1
0
1
0
0
1
1
0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Will be ignored
0 0
0 0
Will be ignored
0 0
0 0
0 0
0 0
SUMMARY
1. Result of Arithmetic Expression is always int type or wider than int type
2. Result of Arithmetic Expression will not be byte , char or short type.
3. When you want to use two same unary Arithmetic Operators continuously then you
must separate those two operators with parenthesis or space.
4. When you use two same unary Arithmetic Operators continuously without
parenthesis or space then it will be considered as INCREMENT or DECREMENT
Operator
5. When you want to use two different unary Arithmetic Operators continuously then
parenthesis or space is not required
6. When char type operators is used in arithmetic expression then ASCII value will be
used while performing the operation
7. Int value can be stored in byte , char or short type variable if it satisfies following
rules
It must be final
It must be initialized in the same statement
0.0/0.0
0/0.00
-0.0/0.0
0.0/0
NaN
NaN
NaN
NaN
13. When you divide any integer value with 0 then it throws
java.lang.ArithmeticException at runtime.
14.Infinity concept is not available with integral type because their range is finity.
15.Infinity concept is available with floating types because their range is near to
infinity.
16.Operator Preceence
7+(6+5*3)-4/2
7+(6+15)-4/2
7+21-4/2
7+21-2
28-2
26
17.Precedence of Arithmetic operators are as following.
Unary +exp -exp
Multiplicative
*/ %
Additive + 18.When multiple operators of same precedence found in an expression then that will
be evaluated from left to right
19.The variable , value or value of an expression must be assigned to compatible type.
byte
short
int
long
float
double
char
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
char ch=(char)a;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Value
->65535