You are on page 1of 37

Basic Elements of Java

By

Dr. K. John Singh

1 VIT - SITE 8/19/2016


Character Set
Characters are used to form the words, numbers and
expressions.
alphabets from A.....Z, a....z

all decimal digits from 0....9


characters , . ; ?
!|/ \ _ $ % # & ^
* - + < > ( ) [ ] { } Spaces

2 VIT - SITE 8/19/2016


Keywords and Identifiers
All keywords must be written in lowercase.

double int else long string extern


super final interface package class
void public if static
Identifiers refer to the names of variables, functions and arrays.
These are user -defined names and consist of a sequence of
letters and digits.

3 VIT - SITE 8/19/2016


Data Types

Data types in Java

Primitive (Intrinsic) Non- Primitive


(Derived)

Numeric Non-Numeric
Classes
Arrays

Character Boolean
Integer Floating Point
(2) (1)
Interface

VIT - SITE
4 8/19/2016
Data Types

Integer

byte (1) short (2) int (4) long (8)

Floating Point

float (4) Double (8)

VIT - SITE
5 8/19/2016
Primitive Data Types

byte E.g.
10
short -15
int 30000
long 555
4567
6 VIT - SITE 8/19/2016
Primitive Data Types
25.6
float 44.567
double -23.46

a
char \u0000
boolean true / false

7 VIT - SITE 8/19/2016


Declaration
byte a = 20;
short b = 22;
int c = 34567;
long l = 34905857;
float e = 324.5f; / 324.5F;
double g = 2234.5; / 2234.5d; / 2234.5D;
char h = a;
boolean = true; / false;

8 VIT - SITE 8/19/2016


Memory Allocation
-Ve +Ve
byte 8bits 28 - 256 -128 -1 |0 - 127

128 128

short 16 bits 216 - 65536 -32768 -1 | 0-32767

int 32 bits 232 - 4,294,967,296

long 64 bits 264 - 18,446,744,073,709,551,616

9 VIT - SITE 8/19/2016


Memory Allocation

float 32 bits
double 64 bits
char 16 bits - Unicode
boolean JVM Dependent

10 VIT - SITE 8/19/2016


Understanding the Storage limit

Ariane 5
11 VIT - SITE 8/19/2016
Default values
byte
short
int
long
0
float
double
char 0.0
- \u0000
boolean - false

12 VIT - SITE 8/19/2016


Type Conversion
byte short int long (implicitly)
byteshortintlong (Explicit casting)
float double (implicit)
floatdouble (explicit)
boolean(Cannot be casted)

13 VIT - SITE 8/19/2016


Type Casting
Implicit Casting
byte c = 120;
short d = c;

Explicit Casting
short c = 234;
byte d = (byte)c;
(We need to explicitly mention to cast)

14 VIT - SITE 8/19/2016


Constants
Constants in java refer to fixed values that do not change during
the execution of a program.
Java supports several types of constants as shown below: -
1. Numeric Constants
Integer Constants
Floating point (real) Constants
2. Character Constants
Single Character Constants
String Constants

15 VIT - SITE 8/19/2016


Integer Constants
Decimal integers consist of a set of digits from 0
through 9. The decimal integers can be positive or
negative.
Octal integers consist of any combination of digits
from 0 through 7, with a leading zero.
Hexadecimal integers consist of a sequence of digits 0
through 9 and alphabets from A (a) through F (f). The
letters 'A' (a) through 'F ' (f) represent the integers 10
through 15.

16 VIT - SITE 8/19/2016


Floating-Point Constants

Floating-point constants are represented by numbers


containing fractional parts like in 549.4545.
Floating-point constants are also sometimes called as real
number constants.

17 VIT - SITE 8/19/2016


Single Character Constants
A character constant is a single character enclosed within a pair of single
quotes.
Example:
'A'
'3'
'?'
';'
''
Constant ASCII value
'a' 97
'A' 65
'&' 38
';' 59
18 VIT - SITE 8/19/2016
String Constants
A string constant is a sequence of characters enclosed
within a pair of double quotes.
The string constant may also include special characters,
numbers and blank spaces.
Example:
" Hello!"
" I'm going for shopping today. Will you come?"
" 549, The Mall, Shimla."

19 VIT - SITE 8/19/2016


Escape Sequences
Character Escape sequence
Octal character (ddd) \ddd
Hexadecimal UNICODE character \uxxxx
Single quote \
Double quote \
Backslash \\
Carriage return \r
New line \n
Tab \t
Backspace \b

20 VIT - SITE 8/19/2016


Variables
A variable is an identifier that is used to represent a
single data item
Declaration
Definition
3 types
1. Instance variables
2. Class variables
3. Local variables

21 VIT - SITE 8/19/2016


Type Casting
Store a value of one type into another type
type variable1 = (type) variable2;
Ex:
int a = 10;
byte n = (byte) m;
float x=12;
int y=(int) x;
Automatic Conversion:
byte a =25;
int a = b;
byte a=10;
float b=(float)a; //not require (lower to higher)

22 VIT - SITE 8/19/2016


Getting the result to the display screen
class display
{
public static void main(String args[])
{
System.out.println(Screen Display);
for(int i=1;i<=9;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print( );
System.out.print(i);
}
System.out.print(\n);
}
System.out.println(Screen Display Done); } }

23 VIT - SITE 8/19/2016


Operators
An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations.
Arithmetic Operators
o Increment and Decrement Operators
Relational and Logical Operators
Assignment Operators
Sizeof Operator
Comma Operator
Conditional Operator (?:)

24 VIT - SITE 8/19/2016


Operators
// Demonstrate the basic arithmetic operators.
class basicMath {
public static void main (string args [ ] ) {
/ / arithmetic using integers
System.out.print1n (Integer Arithmetic)
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c a;
int e = -d ;

25 VIT - SITE 8/19/2016


System.out.println ( a= +a);
System.out.println (b = + b);
System.out.println (c= + c);
System.out.println (d = + d);
System.out.println ( e = + e);
// arithmetic using doubles
System. out. println (\no Floating Point Arithmetic);
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc a;
double de = - dd;
26 VIT - SITE 8/19/2016
System.out.println (da= +da);
System.out.println (db= +db);
System.out.println (dc= +dc);
System.out.println (dd= + dd);
System.out.println (de = +de);
}
When you run this program, you will see the following output.
Integer Arithmetic
a= 2
b= 6
c=1
d = -1
27 VIT - SITE 8/19/2016
e=1
Floating point Arithmetic
da = 2
db = 6
dc = 1.5
dd = -0.5
de = 0.5

28 VIT - SITE 8/19/2016


The following program demonstrates the increment operator.
class IncDec {
public static void main(String a[ ]); The output of this program follows
int a=1; a=2
int b=2; b=3
int c,d; c =4
c = ++b; d=1
d = a++;
c++;
System.out.println(a = +a);
System.out.println(b = +b);
System.out.println(c = +c);
System.out.println(d = +d);
} } VIT - SITE
29 8/19/2016
== Equal to
!= Not Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
expression1? expression1: expression2

30 VIT - SITE 8/19/2016


~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment

31 VIT - SITE 8/19/2016


Control Statements
Conditional Statements
if statement
if-else statement
switch-case statement
Loop Statements
for loop
while loop
do-while loop
Breaking control statements
break
continue
goto

32 VIT - SITE 8/19/2016


Switch statement
switch(expression)
{ case constant_1:
statement;
break;
case constant_2:
statement;
break;
-----------------
case constant_n:
statement,
break;
default:
statement;
break;
}
33 VIT - SITE 8/19/2016
class SwitchTest case 'B':
{ System.out.println ("Value is
public static void main (String B");
args [ ] ) break;
{ default:
char ch = 'A'; System.out.println ("Unknown
switch (ch) Value");
{ }
case 'A': }
System.out.println ("Value is
A");
break;
34 VIT - SITE 8/19/2016
For statement

class ForTest for( i = 0; i <= 10; i++)


{ sum += i;
public static void main System.out.println ("The sum of
(String args [ ] ) first 10 Nos =" + sum );
{ }
int i= 0; }
int sum = 0;

35 VIT - SITE 8/19/2016


While statement
class WhileTest
{
public static void main (String args [ ] )
{
int i=1;
while (i<=5)
{
System.out.println ("i =" + i);
i++;
}}}

36 VIT - SITE 8/19/2016


Do.While and break statements
class BreakLoop
{
public static void main (String args [ ])
{
int i= 0;
do {
System.out.println ("Im stuck !" ) ;
i++;
if (i > 5)
break;
} while (true);
}}

37 VIT - SITE 8/19/2016

You might also like