core java 1.6 1 [email protected]. c lass class is a template for multiple objects with similar...
TRANSCRIPT
jero
me@
caly
donte
ch.co
m
1
CORE JAVA 1.6
2
jero
me@
caly
donte
ch.co
mCLASS Class is a template for multiple objects with
similar features and It is a blue print for objects. It defines a type of object according to the
data the object can hold and the operations the object can perform.
Classes are the fundamental units in an object-oriented programming.
We use a class to create objects( instances ). Each instance carries its own data.
Class Instance
Rubber stamp Stamped image
Photographic negative Printed photo
3
jero
me@
caly
donte
ch.co
mOBJECT Instance of class. Have its own state. Have access to all the behaviors of the class.
4
jero
me@
caly
donte
ch.co
mSTATE Instance variables Attributes of class
5
jero
me@
caly
donte
ch.co
mBEHAVIOR Methods in class Functionalities of object.
6
jero
me@
caly
donte
ch.co
mOBJECT CLASS All classes in JavaTM technology are directly or
indirectly derived from the Object class. Some of the subclasses of Object class are -
Boolean, Number, Void, Math, String, StringBuffer etc.
7
jero
me@
caly
donte
ch.co
mIDENTIFIER Three important points to be noted about
identifiers. Legal Identifiers
Naming Rules Code Conventions
Coding Standards Java Beans Naming Standards.
8
jero
me@
caly
donte
ch.co
mLEGAL IDENTIFIER Start with a letter, a currency symbol($) or
connecting characters( _ ). Don’t use keywords. Case sensitive. Example
Legal Illegal
int _a; int :b;
int _____2_w; int -b;
int $c; int e#;
int _$; int .f;
int 7g;
9
jero
me@
caly
donte
ch.co
mJAVA KEYWORDS
abstract boolean break continue
char class const final
double else extends implements
for goto if native
int interface long return
private protected public synchronized
strictfp super switch void
throws transient try case
assert enum byte default
finally import new short
this volatile catch do
float instanceof package static
throw while
10
jero
me@
caly
donte
ch.co
mSOURCE FILE DECLARATION RULES
Only one public class per source file. Comments can appear any where in tile. Public class name should be the filename. Java is a package centric language so the
package declaration should be the first line. A file belongs to only one package. Import should be given after package
declaration. A file can contain more than one non-public
class. File with no public class can have a file name
does not match any of the classes in the file.
11
jero
me@
caly
donte
ch.co
m
CLASS DECLARATIONS AND MODIFIERS
Modifiers Access Modifiers
public private protected default
Non-Access Modifiers final abstract strictfp
12
jero
me@
caly
donte
ch.co
mDEFAULT OR PACKAGE LEVEL ACCESS
A Class can use public and default access modifiers only.
Has no modifier proceeding in the declaration.
13
jero
me@
caly
donte
ch.co
mDEFAULT OR PACKAGE LEVEL ACCESS
Default access in method level.
14
jero
me@
caly
donte
ch.co
mPUBLIC ACCESS
All classes from all packages can access to the public class.
ie all class in Java Universe can have access to public class.
Three ways to access a method Invoking a method declared in the same class. Invoking a method using a reference of the class. Invoking an inherited method.
15
jero
me@
caly
donte
ch.co
mPRIVATE ACCESS
Members marked private can’t be accessed by code in any class other than the class in which the private members is declared.
16
jero
me@
caly
donte
ch.co
mPROTECTED ACCESS
Protected and default are almost identical with one difference ie protected member can have access to other package classes (through inheritance)
17
jero
me@
caly
donte
ch.co
mACCESS MODIFIERS
Visibility Public Protected Private Default
Same Class Yes Yes Yes Yes
Any Class of Same pack
Yes Yes No Yes
Subclass in same pack Yes Yes No Yes
Subclass outside same pack
Yes Yesinheritance
No No
Not subclass outside pack
Yes No No No
18
jero
me@
caly
donte
ch.co
mNON – ACCESS MODIFIERS
final abstract transient synchronized native – Platform dependent. strictfp – IEEE 754 static.
19
jero
me@
caly
donte
ch.co
mVARIABLE DECLARATIONS
Primitive Variables: Variables with primitive data types such as int or
long. Stores data in the actual memory location of
where the variable is present Reference Variables:
Variables that store the address in the memory location Points to another memory location where the actual data is present
20
jero
me@
caly
donte
ch.co
mPRIMITIVE VARIABLES
Type Bits Bytes Min Range
Max Range
byte 8 1 -27 27 -1
short 16 2 -215 215 -1
int 32 4 -231 231 -1
long 64 8 -263 263 -1
float 32 4 n/a n/a
double 64 8 n/a n/a
Char 16 bit Unicode
Boolean True or false.
21
jero
me@
caly
donte
ch.co
mINSTANCE VARIABLE DECLARATIONS
Defined inside class and outside methods. Initialized when the class is instantiated. Can use any access level. Can be marked as
final transient static volatile
22
jero
me@
caly
donte
ch.co
mLOCAL VARIABLE DECLARATIONS
Variable declaration within a method. Declared, initialized inside the method ie the life
time of local variable will be inside method. Local variable will be on stack not on heap. In case of reference variable it will be created in
heap not in stack. Can not be marked as
public transient volatile abstract static.
Compiler reject if any local variable has not been assigned a value.
If local and instance variable has same name we call it as shadowing.
23
jero
me@
caly
donte
ch.co
mARRAY DECLARATIONS
Object that store multiple variables of the same type.
Can hold primitives or references Array will be as an object on the heap.
Examples
int[ ] key; // recommended
int key[ ]; // less readable
String [ ] [ ] [ ] name;
String [ ] manager[ ];
int [5] scores; // not legal.
24
jero
me@
caly
donte
ch.co
mFINAL VARIABLES
Makes it impossible to reinitialize once it has been initialized with an explicit value.(not default value)
For primitive Once a variable is assigned a value it can’t be
altered. For Reference
Data within the object can be modified. Reference variable cannot be changed.
25
jero
me@
caly
donte
ch.co
mTRANSIENT VARIABLES
If a variable is declared as transient means we are telling the JVM to skip serialization for particular object.
Serialization means writing objects state into streams.
26
jero
me@
caly
donte
ch.co
mVOLATILE VARIABLES
Tells the JVM that a thread accessing the variable must always have its won private copy of the variable with master copy in memory.
Only applied to instance variable.
27
jero
me@
caly
donte
ch.co
mSTATIC VARIABLES Create variables independent of any instance.
Static members exits before making a new instance of class.
Only one copy of static member regardless of member variable.
Area to use
Methods
Variables
Inner classes
Initialization Blocks
Not to use in
Constructors
Classes
Interfaces
Method local inner classes
Inner class methods and variables
Local variables.
28
jero
me@
caly
donte
ch.co
mENUMS Should be declared as separate class or a class member
not inside methods.
Enum cannot be private or protected.
Equalent Java Class
Never invoke an enum directly
Constructor overloading is also possible in enum
29
jero
me@
caly
donte
ch.co
mENUMS
30
jero
me@
caly
donte
ch.co
mENUMS
31
jero
me@
caly
donte
ch.co
mENUMS
32
jero
me@
caly
donte
ch.co
mENCAPSULATION
Encapsulation is the technique of making the fields in a class
private and providing access to the fields via public methods.
If a field is declared private, it cannot be accessed by anyone
outside the class, thereby hiding the fields within the class.
For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that
prevents the code and data being randomly accessed by other
code defined outside the class.
The main benefit of encapsulation is the ability to modify our
implemented code without breaking the code of others who use our
code.
With this feature Encapsulation gives maintainability, flexibility and
extensibility to our code.
33
jero
me@
caly
donte
ch.co
mENCAPSULATION
34
jero
me@
caly
donte
ch.co
mCONSTRUCTOR
Objects are constructed with the help of constructors.
You cannot make a new object without constructor.
Constructors are the code that runs whenever you use
the keyword new.
Every class including abstract class must have a
constructor.
Typically constructors are used to initialize instance
variables.
Two Key Points
No return type
Name should be same as class
35
jero
me@
caly
donte
ch.co
mRULES FOR CONSTRUCTOR
Can use any access modifier
Must match the name of the class
Must not have a return type
If you don’t type a constructor a default constructor will be
automatically generated by the compiler
The default constructor is always a no-args constructor.
Every constructor has its first statement either
this() ->overloaded constructor
super() ->super class constructor
Interface don’t have constructor
Abstract class have constructor that activates in concrete class.
Don’t call a constructor inside any methods.
36
jero
me@
caly
donte
ch.co
mDETERMINE WHETHER DEFAULT CONSTRUCTOR WILL BE CREATED
No!
No!
Yes!
Yes!
37
jero
me@
caly
donte
ch.co
mCHECK WHETHER LEGAL IN CONSTRUCTORS
Legal !
IlLegal !
38
jero
me@
caly
donte
ch.co
mSTATIC VARIABLES & METHODS
In static variable or method the behavior has no dependency on
its instance.
Variables or methods declared as static belongs to class not to
particular instance.
We can use static method or variable without instance of class.
Non static instance variable cannot be referenced inside static
methods because there is no instance.
39
jero
me@
caly
donte
ch.co
mSTATIC VARIABLES & METHODS
If the same program written without static
Here the JVM does not know about frogCount
as it is not static.
40
jero
me@
caly
donte
ch.co
mACCESSING STATIC VARIABLES & METHODS
41
jero
me@
caly
donte
ch.co
mCHECK WHETHER LEGAL IN STATICS
42
jero
me@
caly
donte
ch.co
m
LITERAL VALUES FOR ALL PRIMITIVES
43
jero
me@
caly
donte
ch.co
mINTEGER LITERALS Three ways to represent integer values in
Java Decimal ( base 10 ) Octal ( base 8 ) Hexadecimal ( base 16 )
Decimal Literal Normal integers
Octal Integers Use only digits 0 to 7 We can have up to 21 digits in an octal number
not including the leading 0
44
jero
me@
caly
donte
ch.co
mINTEGER LITERALS Hexadecimal Literal
Constructed using 16 digit symbols Numbers ( 0 to 9 ) Alphabets ( a to f )
All integer literals are defined in int but they can be used in long with the suffix “F”
45
jero
me@
caly
donte
ch.co
mFLOATING POINT LITERALS Floating points are defined as a number, a
decimal and more numbers specifying fraction.
46
jero
me@
caly
donte
ch.co
mBOOLEAN AND CHARACTER LITERALS Boolean Literal
Can hold true or false. Character Literal
Characters are 16 bit unsigned integers It fill fit unsigned int range ( 65535 or less )
47
jero
me@
caly
donte
ch.co
mPRIMITIVE CASTING We can assign a primitive by value and result
of an expression with the help of assignment operator.
Casting lets you convert primitive values from one type to another.
Types of casting Implicit Explicit
Implicit Casting Done by the compiler If we assign a byte value into int implicit casting
is done.
48
jero
me@
caly
donte
ch.co
mPRIMITIVE CASTING Explicit Casting
Look at the following example Rule :
Result of an expression involving anything int-sized or smaller is always int.
Ex, Multiply int and short gives an int. Divide short and byte gives an int.
49
jero
me@
caly
donte
ch.co
mPRIMITIVE CASTING Casting
Saying the JVM I am responsible for any data loss while placing it here.
Why we need casting in this example ?
Byte can hold only up to 127 and here it is 128
Then How come -126 ?
Bit Value of 128 is 10000000As 128 is greater than 127 it’s a integer. Integer has 32 bit so add 24 bits 000000000000000000000000100
00000Once after type cast remove the 24 prefix bits and we will get 10000000The left most bit is for signTo find out the value of a negative using twos complement notationFlip all of the bits and add one gives 01111111 gives -128
Adding 1 in 01111111 give 128 againS
50
jero
me@
caly
donte
ch.co
mARRAY Arrays are objects in java that store multiple
values of same type. Declaring Array of primitives
Declaring Array of References
51
jero
me@
caly
donte
ch.co
mCONSTRUCTING ARRAY Array of primitives
Array of References
52
jero
me@
caly
donte
ch.co
mCONSTRUCTING ARRAY Multi dimensional Array
53
jero
me@
caly
donte
ch.co
mINITIALIZATION BLOCKS
54
jero
me@
caly
donte
ch.co
mWRAPPER CLASS Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding
class instances These classes will be in java.lang package
Primitive type
Wrapper class
Constructor Arguments
byte Byte byte or String
short Short short or String
int Integer int or String
long Long long or String
float Float float, double or String
double Double double or String
char Character char
boolean Boolean boolean or String
55
jero
me@
caly
donte
ch.co
mWRAPPER CLASS Class to min and max values of primitives
56
jero
me@
caly
donte
ch.co
mWRAPPER CLASS Class to print default values of primitives
57
jero
me@
caly
donte
ch.co
m
58
jero
me@
caly
donte
ch.co
mWRAPPER CLASS
59
jero
me@
caly
donte
ch.co
mWRAPPER CLASS – AUTO BOXING