3 Java Fundamentals
3 Java Fundamentals
Mr. M.R.
Solanki
Sr. Lecturer, Information Technology,
manish_ratilal2002@yahoo.com
SBMP
Learning Outcomes
“
We can not write the following statement for
characters:
char ch=‘A’;
ch=ch+1; //Error
Whereas, ch++ or ch+=1 is allowed
Primitive Data Types: boolean
Java has a primitive type boolean for logical
values.
int a=5,b=7;
System.out.print(a>b); // false # in
C/C++ =>0
System.out.print(a<b); // true # in C/C+
+ =>1
System.out.print(a!=b); // true
“
The result of the condition used
conditional and looping statements
in
is
boolean only .
If(num%2==0)
while(i<=n)
{
// stetements
}
Primitive Data Types: boolean
Primitive Data Types: boolean
Applications :
Used by Java internally as a result of all relational
operators
Literals ( Constants)
integer i.e. 123, -23, 10101, etc.
floating point: 2.546, 0.00023, etc.
Character : ‘A’, ‘#’, etc.
String: “Hello” , “Java” ,etc.
Variables : Rules are same as C/C++
(except $ sign is allowed in Java)
Keywords : Reserved words i.e. class,
float, if, for, etc.
Java Keywords
256 128 64 32 16 8 4 2 1
1 0 0 0 0 0 0 0 1
Solution:
byte b = 50;
b = (byte)(b * 2);
In Java
“
Integral literals are always treated as int while
evaluating expressions.
Floating point literals are always treated as
double.
Floating point literal Error
casting
Operators
Operators
Arithmetic Operators:
Increment/Decrement Operator:
If you use the || and && forms, rather than the | and &
forms of these operators:
Java will not bother to evaluate the right hand operand when
the outcome of the expression can be determined by the left
operand alone.
Operators
Short Circuit Logical(boolean) Operators:
Example1: if(a>b && a<c)
Example2: if (denom != 0 && num / denom > 10)
“
Logical operator = &, |
Short circuit logical operator = &&, ||
Operators
Bitwise Operators:
Operators
Bitwise Operators(~) : Complements every bit of
the value i.e. 0 to 1 and 1 to 0 (NOT gate in Digital
Electronics)
Explanation:
Note: As a is of type byte, we have to
consider 1 byte data
Represent 5 into binary = 0000 0101
Represent 4 into binary = 0000 0100
0 0 0 0 0 1 0 1 0
0 0 0 0 0 0 1 0 1
Least significant bit will be discarded and empty space is filled with 0
Operators
Bitwise Operators: >> (shift right)
Most Significant bit of previous value is copied to
the vacant place. Used to preserve the sign of
previous value.
Example: byte a=-14;
System.out.println(a>>1);
Explanation:
Note: As a is of type byte, we have to
consider 1 byte data 1 1 1 1 0 0 1 0
Represent 5 into binary =
a>>1 =
1 1 1 1 1 0 0 1 0
Least significant bit will be discarded and empty space is filled with
the msb of the previous value.
“
While performing <<, value becomes double.
While performing >>, the value becomes
half.
Operators
Operators
Operators
Operator Precedence:
Control Structures
Control Statements
Conditional Statements:
Simple if
If …..else
nested if
else if ladder
switch…… case
Iterative Statements:
while loop
do.....while loop
for loop
Taking user input using
Scanner class
Scanner class
In Java, there is no function i.e. scanf() or cin as
available in C and C++ respectively.
One of the ways to take user input in Java is
using Scanner class.
type a = sc.nextType();
Scanner class Methods
Method Purpose
nextByte(), nextShort(), nextInt() To read byte, short, int and long
and nextLong() data respectively
nextFloat() and nextDouble() To read float and double data
respectively
Initializing a string:
String str=“Hello”;
System.out.println(str);
String str;
System.out.println(“Enter the String”);
str = sc.nextLine();
board
Keyboard
“
If we read a string after reading integer type
data then provide one dummy statement i.e.
sc.nextLine()
Thanks!
Any questions?
You can find me at:
manish_ratilal2002@yahoo.com
Credits
Java, The Complete Reference, TMH
Tutorials and Forums available on Inte
rnet