Presentation is loading. Please wait.

Presentation is loading. Please wait.

CORE JAVA 1.6 1 C LASS Class is a template for multiple objects with similar features and It is a blue print for objects. It defines.

Similar presentations


Presentation on theme: "CORE JAVA 1.6 1 C LASS Class is a template for multiple objects with similar features and It is a blue print for objects. It defines."— Presentation transcript:

1 CORE JAVA 1.6 1 jerome@calydontech.com

2 C LASS 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. 2 jerome@calydontech.com ClassInstance Rubber stampStamped image Photographic negativePrinted photo

3 O BJECT Instance of class. Have its own state. Have access to all the behaviors of the class. 3 jerome@calydontech.com

4 S TATE Instance variables Attributes of class 4 jerome@calydontech.com

5 B EHAVIOR Methods in class Functionalities of object. 5 jerome@calydontech.com

6 O BJECT C LASS All classes in Java TM 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. 6 jerome@calydontech.com

7 I DENTIFIER Three important points to be noted about identifiers. Legal Identifiers Naming Rules Code Conventions Coding Standards Java Beans Naming Standards. 7 jerome@calydontech.com

8 L EGAL I DENTIFIER Start with a letter, a currency symbol($) or connecting characters( _ ). Don’t use keywords. Case sensitive. Example 8 jerome@calydontech.com LegalIllegal int _a;int :b; int _____2_w;int -b; int $c;int e#; int _$;int.f; int 7g;

9 J AVA K EYWORDS abstractbooleanbreakcontinue charclassconstfinal doubleelseextendsimplements forgotoifnative intinterfacelongreturn privateprotectedpublicsynchronized strictfpsuperswitchvoid throwstransienttrycase assertenumbytedefault finallyimportnewshort thisvolatilecatchdo floatinstanceofpackagestatic throwwhile 9 jerome@calydontech.com

10 S OURCE F ILE D ECLARATION R ULES 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. 10 jerome@calydontech.com

11 C LASS D ECLARATIONS AND M ODIFIERS Modifiers Access Modifiers public private protected default Non-Access Modifiers final abstract strictfp 11 jerome@calydontech.com

12 D EFAULT OR P ACKAGE L EVEL A CCESS A Class can use public and default access modifiers only. Has no modifier proceeding in the declaration. 12 jerome@calydontech.com

13 D EFAULT OR P ACKAGE L EVEL A CCESS Default access in method level. 13 jerome@calydontech.com

14 P UBLIC A CCESS 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. 14 jerome@calydontech.com

15 P RIVATE A CCESS Members marked private can’t be accessed by code in any class other than the class in which the private members is declared. 15 jerome@calydontech.com

16 P ROTECTED A CCESS Protected and default are almost identical with one difference ie protected member can have access to other package classes (through inheritance) 16 jerome@calydontech.com

17 A CCESS M ODIFIERS VisibilityPublicProtectedPrivateDefault Same ClassYes Any Class of Same packYes NoYes Subclass in same packYes NoYes Subclass outside same pack Yes inheritance No Not subclass outside pack YesNo 17 jerome@calydontech.com

18 N ON – A CCESS M ODIFIERS final abstract transient synchronized native – Platform dependent. strictfp – IEEE 754 static. 18 jerome@calydontech.com

19 V ARIABLE D ECLARATIONS 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 19 jerome@calydontech.com

20 P RIMITIVE V ARIABLES TypeBitsBytesMin RangeMax Range byte81-2 7 2 7 -1 short162-2 15 2 15 -1 int324-2 31 2 31 -1 long648-2 63 2 63 -1 float324n/a double648n/a 20 jerome@calydontech.com Char 16 bit Unicode Boolean True or false.

21 I NSTANCE V ARIABLE D ECLARATIONS 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 21 jerome@calydontech.com

22 L OCAL V ARIABLE D ECLARATIONS 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. 22 jerome@calydontech.com

23 A RRAY D ECLARATIONS Object that store multiple variables of the same type. Can hold primitives or references Array will be as an object on the heap. 23 jerome@calydontech.com Examples int[ ] key; // recommended int key[ ]; // less readable String [ ] [ ] [ ] name; String [ ] manager[ ]; int [5] scores; // not legal.

24 F INAL V ARIABLES 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. 24 jerome@calydontech.com

25 T RANSIENT V ARIABLES 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. 25 jerome@calydontech.com

26 V OLATILE V ARIABLES 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. 26 jerome@calydontech.com

27 S TATIC V ARIABLES 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. 27 jerome@calydontech.com

28 E NUMS 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 28 jerome@calydontech.com

29 E NUMS 29 jerome@calydontech.com

30 E NUMS 30 jerome@calydontech.com

31 E NUMS 31 jerome@calydontech.com

32 E NCAPSULATION 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. 32 jerome@calydontech.com

33 E NCAPSULATION 33 jerome@calydontech.com

34 C ONSTRUCTOR 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 34 jerome@calydontech.com

35 R ULES F OR C ONSTRUCTOR 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. 35 jerome@calydontech.com

36 D ETERMINE W HETHER D EFAULT C ONSTRUCTOR W ILL B E C REATED 36 jerome@calydontech.com No! Yes!

37 C HECK W HETHER L EGAL I N C ONSTRUCTORS 37 jerome@calydontech.com Legal ! IlLegal !

38 S TATIC V ARIABLES & M ETHODS 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. 38 jerome@calydontech.com

39 S TATIC V ARIABLES & M ETHODS If the same program written without static Here the JVM does not know about frogCount as it is not static. 39 jerome@calydontech.com

40 A CCESSING S TATIC V ARIABLES & M ETHODS 40 jerome@calydontech.com

41 C HECK W HETHER L EGAL I N S TATICS 41 jerome@calydontech.com

42 L ITERAL V ALUES F OR A LL P RIMITIVES 42 jerome@calydontech.com

43 I NTEGER L ITERALS 43 jerome@calydontech.com 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 I NTEGER L ITERALS 44 jerome@calydontech.com 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 F LOATING P OINT L ITERALS 45 jerome@calydontech.com Floating points are defined as a number, a decimal and more numbers specifying fraction.

46 B OOLEAN AND C HARACTER L ITERALS 46 jerome@calydontech.com 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 P RIMITIVE C ASTING 47 jerome@calydontech.com 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 P RIMITIVE C ASTING 48 jerome@calydontech.com 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 P RIMITIVE C ASTING 49 jerome@calydontech.com 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 10000000 As 128 is greater than 127 it’s a integer. Integer has 32 bit so add 24 bits 00000000000000000000000010000000 Once after type cast remove the 24 prefix bits and we will get 10000000 The left most bit is for sign To find out the value of a negative using twos complement notation Flip all of the bits and add one gives 01111111 gives -128 Adding 1 in 01111111 give 128 againS

50 A RRAY 50 jerome@calydontech.com Arrays are objects in java that store multiple values of same type. Declaring Array of primitives Declaring Array of References

51 C ONSTRUCTING A RRAY 51 jerome@calydontech.com Array of primitives Array of References

52 C ONSTRUCTING A RRAY 52 jerome@calydontech.com Multi dimensional Array

53 I NITIALIZATION B LOCKS 53 jerome@calydontech.com

54 W RAPPER CLASS 54 jerome@calydontech.com 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 typeWrapper classConstructor Arguments byteBytebyte or String shortShortshort or String intIntegerint or String longLonglong or String floatFloatfloat, double or String doubleDoubledouble or String charCharacterchar booleanBooleanboolean or String

55 W RAPPER CLASS 55 jerome@calydontech.com Class to min and max values of primitives

56 W RAPPER CLASS 56 jerome@calydontech.com Class to print default values of primitives

57 57 jerome@calydontech.com

58 W RAPPER CLASS 58 jerome@calydontech.com

59 W RAPPER CLASS – A UTO B OXING 59 jerome@calydontech.com


Download ppt "CORE JAVA 1.6 1 C LASS Class is a template for multiple objects with similar features and It is a blue print for objects. It defines."

Similar presentations


Ads by Google