10
10
10
=======================
1. OOPS(basic introduction)
2. Identifier/variables
a. rules to write an identifier
3. Reserved words
4. Data types and its chart.
OOPS
--------
It stands for Object Orientation Principles.
Object -> real time instance or an entity.
eg: Car,Student,Employee
Every object in realtime will have 2 parts
what it has
what it does
eg: Car
brandName
noOfWheels
model
speed
move
accelerate
brake
Java code
-------------
class Car
{
//HAS part of an Object is represented as a "variable".
String brandName;
int noOfWheels;
#2.
class Student
{
//HAS-part (variables)
String name;
int id;
float height;
//DOES-part(methods)
public void study(){
//logic of studying
}
public void play(){
//logic of playing
}
}
Identifier
========
It is a name in java program.
It can be a classname,methodname,variable name and label name.
class Test{
public static void main(String[] args){
int x= 10;
}
}
Totally 5 identifers
eg#2.
class Test{
public static void main(String[] args){
System.out.println("sachin");
}
}
Totally 7 identifiers.
Rule2: If we use any other characters it would result in compile time error
int ^* = 10;(invalid)
int total = 10;(valid)
int total#= 35;(invalid)
Rule4: Java identifiers are case sensitive,meaning number and Number is different.
class Demo{
int number=10;
int Number=20;
int nUmber= 30;
int NUMBER = 40;
}
eg#1
String Runnable = "sachin";
System.out.println(Runnable);//sachin
eg#2
int String = 10;
System.out.println(String);//10
Note: Even though predefined class names can be used as an indetifiers,it is not a
good practise to keep.
Interview Question
-------------------------
int If =10;//if and If is different
System.out.println(If);//10
ReservedWords
--------------------
It is a built in words/keywords which has already a predefined meaning to it.
refer: Reservewords.png
Note:
Literal Any constant value which can be assigned to a variable is called literal
int data =10;
literal -> 10
data -> variableName/identifier
int -> datatype/reserveword
Note: for boolean datatypes the only values allowed for a variable is "true,false",
other than this if we try
to keep any values it would reslut in "CompileTimeError".
=> All reserved words names would start with "lower case ".
=> In java all Classnames/interfacenames would start with "upper case".
2. break,continue,return,exit
ans. exit is not a reserve word, it is a method in System class
3. byte,short,Integer,long
ans. Integer is not a reserve word,it is a predefined class
4. throw,throws,thrown
ans. thrown is not a reserve word,it is a userdefined variable.
Datatypes
--------------
Every varaible has a type,every expression has a type and all types are strictly
typed/define in java
becoz java is strictlytype /statically typed language.
Compiler role -> Compiler will check the value stored can be handled by
datatype or not
This checking which is done by compiler is
called "TypeChecking/Strictlytype checking".
Primtive datatypes
================
meaning -> data which is commonly used and supported by any langauge to store
directly.
a.Numeric values
=> to store number
a. whole number
b. realnumber
b.character values
=> to store character type of data
c.boolean values
=> to store logical values
Number data
------------------
To store whole numbers we have 4 datatypes
a. byte
b. short
c. int
d. long
note:
System.out.println("Size of byte is :: "+Byte.SIZE);
System.out.println("MINVALUE of byte is :: "+Byte.MIN_VALUE);
System.out.println("MAXVALUE of byte is :: "+Byte.MAX_VALUE);
byte:
size -> 8 bits
minvalue -> -128
maxvalue -> 127
eg:
byte marks=35 //valid
byte marks = 135; //CE: possible loss of precission
byte marks = -1;//valid
short
System.out.println("Size of short is :: "+Short.SIZE);
System.out.println("MINVALUE of short is :: "+Short.MIN_VALUE);
System.out.println("MAXVALUE of short is :: "+Short.MAX_VALUE);
eg:
short data=137;//valid
short data = true; //CE: incompatible types
short data = "sachin";//CE:incompatible types
Note: This data is not at all used in java and this data type is best sutied only
if u have old processors like 8086.
int:
System.out.println("Size of int is :: "+Integer.SIZE);
System.out.println("MINVALUE of int is :: "+Integer.MIN_VALUE);
System.out.println("MAXVALUE of intt is :: "+Integer.MAX_VALUE);
note: The most commonly used datatype for storing whole number is "int" only and by
default if we specify any literal of
number type compiler will try to keep it as "int" only,but we can keep
either in short or byte also.
long
System.out.println("Size of long is :: "+Long.SIZE);
System.out.println("MINVALUE of long is :: "+Long.MIN_VALUE);
System.out.println("MAXVALUE of long is :: "+Long.MAX_VALUE);
size: 64bits(8bytes)
minvalue:-9223372036854775808
maxvalue:9223372036854775807
If the data goes beyond the range of int,then to keep the data inside long
data type we need to explicitly suffix the
data with 'L' or 'l' otherwise it would result in "CompileTimeError".
eg: long firstData=9223372036854775807;//CE
long secodData=9223372036854775807;//CE
long firstData=9223372036854775807L;
long secodData=9223372036854775807l;
Note:
When int is not enough to hold the big values, then we use long data type.
When we work with large files, data would come to java program in terms of
GB's.
long size = file.length();