CORE JAVA
What is Java
Java is a programming language and a platform. Java is a high level,
1 robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems in the year 1995. James Gos-
ling is known as the father of Java. Before Java, its name was Oak. Since
2 Oak was already a registered company, so James Gosling and his team
changed the name from Oak to Java.
It is used for:
3 Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
Java Syntax
Syntax
class Name {
public static void
main(String[] args)
{
} }
Java First Program
Javaapplication29 is a identifier
and identify your class name .
Public show your program ac-
cess and this is reserved words.
String is predefine class or data
types and this is identifier
모바일 이미지
Java first Program Println is predefine keyword
(anything print on screen)
Java Identifiers
Identifiers:
A name in java program is called identifier.
It may be class name, method name, vari-
able name and label name.
Unique
All Java variables must
be identified with unique names
Names
Identifiers can be short names (like x
and y) or more descriptive names
(age, sum, totalVolume).
Example of Java Identifiers
Test:
Class name.
args:
variable name.
Main:
Method name.
String: a:
Predefined class variable name.
name.
Rules of Identifiers
Rule 1: Rule 3:
1 The only allowed characters in java
identifiers are: 3 Identifiers are not allowed to starts with
digit.
1) a to z
2) A to Z Example:
3) 0 to 9 1) ABC123---------valid
4) _ (underscore) 2) 123ABC---------invalid
5) $
Rule 4:
2
Rule 2:
If we are using any other character we
4 Java identifiers are case sensitive up course java
language itself treated as case sensitive language.
will get compile time error.
Example:
Example: class Test{
1) Saad_iftikhar-------valid int number=10;
2) Saadl#------------------invalid int Number=20;
int NUMBER=20; we can differentiate with case.
int NuMbEr=30;
}
Rules of Identifiers
Rule 5: Rule 3:
5 There is no length limit for java identi-
fiers but it is not recommended to take 7 All predefined java class names and interface names
we use as identifiers.
more than 15 lengths. Example 1:
class Test
{
public static void main(String[] args){
6
Rule 6: int String=10;
We can't use reserved words as identi- System.out.println(String);
fiers. }}
Output:
Example: 10
int if=10; --------------invalid Example 2:
class Test
{
public static void main(String[] args){
Note: int Runnable=10;
Even though it is legal to use class names and in- System.out.println(Runnable);
terface names as identifiers but it is not }}
a good programming practice. Output:
10
Which of the following are valid java identifiers?
Examples of Valid Identifiers: Examples of Invalid Identifiers:
myVariable 1variable (starts with a digit)
_myVariable my-variable (hyphens are not allowed)
$myVariable my variable (spaces are not allowed)
myVariable1 class (reserved keyword)
my_variable public (reserved keyword)
MY_VARIABLE void (reserved keyword)
aVeryLongVariableNameThatIsStillValid #myVariable (special character # is not allowed)
__init__ @myVariable (special character @ is not allowed)
Reserved words
Reserved words:
Keyword’s are the reserved word
whose meaning is already defined in
the compiler called keywords or
reserved words.
In java some identifiers are reserved to
associate some functionality or meaning
such type of reserved identifiers are
called reserved words.
Reserved words
Reserved Words:
There are 53 reserved words.
true Use for Boolean values
50 keywords.
3 Reserved literals false
null Use for Object
Reserved
Literals
3
Reserved Content Here Used
Words 53 You can simply im- Keyword
press your audience 48
and add a unique
zing. goto
Keyword const
50 Unused
Keyword
2
Reserved Words
Reserved words for data types: (8) Reserved words for flow control:(11)
1) byte 1) if
2) short 2) else
3) int 3) switch
4) long 4) case
5) float 5) default
6) double 6) for
7) char 7) do
8) boolean 8) while
9) break
10) continue
11) return
Reserved Words
Keywords for modifiers:(11) Keywords for exception handling:(6)
1) public 1) try
2) private 2) catch
3) protected 3) finally
4) static 4) throw
5) final 5) throws
6) abstract 6) assert(1.4 version)
7) synchronized
8) native
9) strictfp(1.2 version)
10) transient
11) volatile
Reserved Words
Class related keywords:(6) Object related keywords:(4)
1) class 1) new
2) package 2) instanceof
3) import 3) super
4) extends 4) this
5) implements
6) interface
Enum:
This keyword introduced in 1.5v to define a group of named constants
Example:
enum Beer
{
KF, RC, KO, FO;
}
Reserved Words
Void return type keyword:
If a method won't return anything compulsory that method should be declared with the
void return type in java but it is optional in C++.
1) void
Unused keywords:
goto: Create several problems in old languages and hence it is banned in java.
Const: Use final instead of this.
By mistake if we are using these keywords in our program we will get compile time
error.
Reserved literals:
1) true values for boolean data type.
2) false
3) null----------------- default value for object reference.
Conclusions :
1. All reserved words in java contain only lowercase alphabet symbols.
2. New keywords in java are:
3. strictfp-----------1.2v
4. assert-------------1.4v
5. enum--------------1.5v
6. In java we have only new keyword but not delete because destruction of useless
objects is the responsibility of Garbage Collection.
7. instanceof but not instanceOf
8. strictfp but not strictFp
9. const but not Constant
10. syncronized but not syncronize
11. extends but not extend
12. implements but not implement
13. import but not imports
14. int but not Int
Data Types:
Every variable has a type, every expression has a type and all types are strictly define
more over every assignment should be checked by the compiler by the type
compatibility hence java language is considered as strongly typed programming
language.
Java is pure object oriented programming or not?
Java is not considered as pure object oriented programming language because several
oops features (like multiple inheritance, operator overloading) are not supported by
java moreover we are depending on primitive data types which are non objects.
Diagram:
Data Types:
Note:
Except Boolean and char all re-
maining data types are considered
as signed data types because we
can represent both "+ve" and"-ve"
numbers.
Integral Data Types :
Byte:
Size: 1byte (8bits)
Maxvalue: +127
Minvalue:-128
Range:-128to 127[-27 to 27-1]
• The most significant bit acts as sign bit. "0" means "+ve" number and "1" means "–
ve" number.
• "+ve" numbers will be represented directly in the memory whereas "–ve" numbers
will be represented in 2's complement form.
Integral Data Types :
Example:
byte b=10;
byte b2=130; //C.E:possible loss of precision
found : int
required : byte
byte b=10.5; //C.E:possible loss of precision
byte b=true; //C.E:incompatible types
byte b="ashok"; //C.E:incompatible types
found : java.lang.String
required : byte
byte data type is best suitable if we are handling data in terms of streams either from
the file or from the network. ]
Integral Data Types :
Short:
The most rarely used data type in java is short.
Size: 2 bytes
Range: -32768 to 32767(-215 to 215-1)
Example:
short s=130;
short s=32768; //C.E:possible loss of precision
short s=true; //C.E:incompatible types
Short data type is best suitable for 16 bit processors like 8086 but these processors
are completely outdated and hence the corresponding short data type is also out
data type.
Integral Data Types :
Int:
This is most commonly used data type in java.
Size: 4 bytes
Range:-2147483648 to 2147483647 (-231 to 231-1)
Example:
int i=130;
int i=10.5;//C.E:possible loss of precision
int i=true;//C.E:incompatible types
Integral Data Types :
Long:
Whenever int is not enough to hold big values then we should go for long data type.
Example:
To hold the no. Of characters present in a big file int may not enough hence the return
type of length() method is long.
long l=f.length(); //f is a file
Size: 8 bytes
Range:-263 to 263-1
Note: All the above data types (byte, short, int and long) can be used to represent
whole numbers. If we want to represent real numbers then we should go for floating
point data types.
Floating Point Data types:
Float Double
If we want to 5 to 6 decimal places of If we want to 14 to 15 decimal places of
accuracy then we should go for float. accuracy then we should go for double.
Size:4 bytes. Size:8 bytes
Range:-3.4e38 to 3.4e38. -1.7e308 to1.7e308.
float follows single precision. Double follows double precision.
Boolean data type: :
Size: Not applicable (virtual machine dependent)
Range: Not applicable but allowed values are true or false.
Which of the following boolean declarations are valid?
Example 1:
boolean b=true;
boolean b=True;//C.E:cannot find symbol
boolean b="True";//C.E:incompatible types
boolean b=0;//C.E:incompatible types
Char data type: :
In old languages like C & C++ are ASCII code based the no.Of ASCII code characters
are < 256 to represent these 256 characters 8 - bits enough hence char size in old
languages 1 byte.
In java we are allowed to use any worldwide alphabets character and java is Unicode
based and no of unicode characters are > 256 and <= 65536 to represent all these
characters one byte is not enough compulsory we should go for 2 bytes.
Size: 2 bytes
Range: 0 to 65535
Example:
char ch1=97;
char ch2=65536; //C.E:possible loss of precision
Summary of java primitive data type: