Module 3   Identifiers, Keywords, and Types  
<ul><li>Objectives </li></ul><ul><ul><li>•  Using comments in a source program  </li></ul></ul><ul><ul><li>•  Distinguish ...
<ul><li>Comments </li></ul><ul><li>Three permissible style of comment in a Java technology  </li></ul><ul><li>program are:...
<ul><li>Identifiers </li></ul><ul><ul><li>•  Are names given to a variable, class, or method </li></ul></ul><ul><ul><li>• ...
Java Keywords
<ul><ul><li>DataType   </li></ul></ul><ul><ul><li>Every variable must have a data type.  </li></ul></ul><ul><ul><li>A vari...
Primitive Types •  The Java programming language defines eight  primitive types:    Logical   boolean Textual   char  Inte...
<ul><li>Logical – boolean </li></ul><ul><ul><li>•  The boolean data type has two literals, true and  </li></ul></ul><ul><u...
<ul><li>Textual – char and String </li></ul><ul><li>char </li></ul><ul><ul><li>•  Represents a 16-bit Unicode character  <...
<ul><li>Textual – char and String </li></ul><ul><li>String </li></ul><ul><ul><li>•  Is not a primitive data type; it is a ...
<ul><li>Integral – byte, short, int, and   long </li></ul><ul><li>•  Uses three forms – Decimal, octal, or hexadecimal  </...
Integral – byte, short, int, and   long Integral data types have the following ranges: Integer  Length  Name or Type  Rang...
<ul><li>Floating Point – float and double </li></ul><ul><ul><li>•  Default is double </li></ul></ul><ul><ul><li>•  Floatin...
<ul><li>Floating Point – float and double </li></ul><ul><li>•  Floating point data types have the following ranges: </li><...
<ul><li>Variables, Declarations, and   Assignments </li></ul><ul><ul><li>public class Assign { </li></ul></ul><ul><ul><ul>...
<ul><ul><li>Java Reference Types </li></ul></ul><ul><ul><li>•  Beyond primitive types all others are reference types </li>...
<ul><li>Constructing and Initializing Objects </li></ul><ul><li>•  Calling new Xxx() to allocate space for the new object ...
 
 
Example OverloadCons.java
 
 
 
 
<ul><li>The this Reference </li></ul><ul><ul><li>Here are a few uses of the this keyword: </li></ul></ul><ul><ul><li>•   t...
<ul><li>The this Reference </li></ul><ul><ul><li>public class MyDate { </li></ul></ul><ul><ul><li>private int day = 1; </l...
Java Coding Conventions •  Packages:  package banking.domain; •  Classes:  The first letter should be capitalized and if s...
Java Coding Conventions •  Methods:  The first letter should be lowercase, and then normal, the  name should typically be ...
Access Modifiers Modifiers are Java Keywords that give the compiler  information about the nature of code ,data or classes...
<ul><ul><li>Public </li></ul></ul><ul><ul><li>Public feature is accessed by any class. </li></ul></ul><ul><ul><li>An apple...
<ul><ul><li>Private </li></ul></ul><ul><ul><li>The least generous access modifier is private . </li></ul></ul><ul><ul><li>...
<ul><ul><li>Default </li></ul></ul><ul><ul><li>Default is the name given to access level that results </li></ul></ul><ul><...
<ul><li>Protected </li></ul><ul><ul><li>A protected feature of class is available to all classes  </li></ul></ul><ul><ul><...
<ul><li>Final </li></ul><ul><li>Final modifiers applies to classes,method and  </li></ul><ul><li>variables. </li></ul><ul>...
<ul><li>Note  Final method may not be overridden. </li></ul><ul><li>Will the  following code compile. </li></ul><ul><ul><l...
<ul><li>Abstract </li></ul><ul><ul><li>Abstract modifier can be applied to classes and  </li></ul></ul><ul><ul><li>methods...
<ul><li>The class is declared abstract if any of the following  </li></ul><ul><li>conditions are true </li></ul><ul><ul><l...
Abstract class vs. Interface Interface   Abstract class A class may implement   A class may extend  several interfaces.  o...
If you add a new method to  If you add a new method to an  an interface, you must track    abstract class, you have the  d...
<ul><li>Static </li></ul><ul><ul><li>This can be applied to variables , methods  </li></ul></ul><ul><ul><li>and a strange ...
Example   Ecstatic.java   Class Ecstati c{ static{   System.out.println(“Hello”); } static int  x=0; Ecstati c(){ x++; } }
<ul><ul><li>Reference a static variable via two ways  </li></ul></ul><ul><ul><li>·        Via reference to instance of cla...
Via class name Ecstatic e1=new Ecstatic(); Ecstatic e2=new Ecstatic(); Ecstatic.x=100; Ecstatic.x=200; reallyImportantVari...
<ul><ul><li>Native </li></ul></ul><ul><ul><li>This modifier can be applied only to methods. </li></ul></ul><ul><ul><li>Lik...
<ul><ul><li>When the native method is invoked the library that contain the native code is loaded and  available to JVM, if...
Transient This modifier applies only to variables. The transient variable is not stored as a part of its object  persisten...
<ul><li>Now if instance of this class is written to a file  </li></ul><ul><li>somebody could snoop the access code. </li><...
<ul><ul><li>Synchronized </li></ul></ul><ul><ul><li>Methods can be declared synchronized. </li></ul></ul><ul><ul><li>This ...
<ul><ul><li>Strictfp </li></ul></ul><ul><ul><li>strictfp forces floating points to adhere to the IEEE 754 standard.  </li>...
Summary   Modifier Class   Variable  Method  Constructor public yes yes yes yes protected no yes yes yes default yes yes y...
 
<ul><li>Which of the following declarations are illegal?  </li></ul><ul><li>(Choose all that   apply.) </li></ul><ul><ul><...
<ul><ul><li>Answer : A, D, E. A is illegal because “default” is not  </li></ul></ul><ul><ul><li>a keyword. B is   a legal ...
<ul><ul><li>Question .  Which of the following statements is true? </li></ul></ul><ul><ul><li>A. Transient methods may not...
<ul><ul><li>Answer : E. </li></ul></ul><ul><ul><li>A, B, and C don’t mean anything, because  </li></ul></ul><ul><ul><li>on...
Upcoming SlideShare
Loading in...5
×

Md03 - part3

169

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
169
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Md03 - part3

  1. 1. Module 3 Identifiers, Keywords, and Types  
  2. 2. <ul><li>Objectives </li></ul><ul><ul><li>• Using comments in a source program </li></ul></ul><ul><ul><li>• Distinguish between valid and invalid identifiers </li></ul></ul><ul><ul><li>• Recognize Java technology keywords </li></ul></ul><ul><ul><li>• List the eight primitive types </li></ul></ul><ul><ul><li>• Declare variables of class type </li></ul></ul><ul><ul><li>• Construct an object using new </li></ul></ul><ul><ul><li>• Describe default initialization </li></ul></ul><ul><ul><li>• Describe the significance of a reference variable </li></ul></ul><ul><ul><li>• State the consequences of assigning variables </li></ul></ul><ul><ul><li>of class type </li></ul></ul><ul><ul><li>• Using Modifiers </li></ul></ul>
  3. 3. <ul><li>Comments </li></ul><ul><li>Three permissible style of comment in a Java technology </li></ul><ul><li>program are: </li></ul><ul><ul><ul><ul><li>// comment on one line. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/* comment on one or </li></ul></ul></ul></ul><ul><ul><ul><ul><li>more line */ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/** documentation comment */ </li></ul></ul></ul></ul>
  4. 4. <ul><li>Identifiers </li></ul><ul><ul><li>• Are names given to a variable, class, or method </li></ul></ul><ul><ul><li>• Can start with a Unicode letter, underscore(_), or dollar </li></ul></ul><ul><ul><li>sign($) </li></ul></ul><ul><ul><li>• Are case sensitive and have no maximum length </li></ul></ul><ul><ul><li>• Keywords cannot be used as identifiers. </li></ul></ul><ul><ul><li>• Identifiers in Java are case sensitive, foo and Foo are two </li></ul></ul><ul><ul><li>different identifiers. </li></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><li>Illegal identifiers legal identifier </li></ul></ul><ul><ul><li>int :b; userName </li></ul></ul><ul><ul><li>int -d; user_name </li></ul></ul><ul><ul><li>int e#; _sys_var1 </li></ul></ul><ul><ul><li>int .f; $change </li></ul></ul><ul><ul><li>int 7g; </li></ul></ul>
  5. 5. Java Keywords
  6. 6. <ul><ul><li>DataType </li></ul></ul><ul><ul><li>Every variable must have a data type. </li></ul></ul><ul><ul><li>A variable's data type determines the values that the </li></ul></ul><ul><ul><li>variable can contain and the operations that can be </li></ul></ul><ul><ul><li>performed on it. </li></ul></ul><ul><ul><li>There are two categories of data types: </li></ul></ul><ul><ul><li>primitive and reference. </li></ul></ul>
  7. 7. Primitive Types • The Java programming language defines eight primitive types: Logical boolean Textual char Integral byte, short, int, and long Floating double and float
  8. 8. <ul><li>Logical – boolean </li></ul><ul><ul><li>• The boolean data type has two literals, true and </li></ul></ul><ul><ul><li>false. </li></ul></ul><ul><ul><li>For example, the statement: </li></ul></ul><ul><ul><li>boolean truth = true; </li></ul></ul><ul><ul><li>declares the variable truth as boolean type and </li></ul></ul><ul><ul><li>assigns it a value of true. </li></ul></ul>
  9. 9. <ul><li>Textual – char and String </li></ul><ul><li>char </li></ul><ul><ul><li>• Represents a 16-bit Unicode character </li></ul></ul><ul><ul><li>• Must have its literal enclosed in single quotes(’ ’) </li></ul></ul><ul><ul><li>• Uses the following notations: </li></ul></ul><ul><ul><li>'a' The letter a </li></ul></ul><ul><ul><li>• It has a minimum value of 'u0000' (or 0) and a maximum </li></ul></ul><ul><ul><li>value of 'uffff' (or 65,535 inclusive). </li></ul></ul>
  10. 10. <ul><li>Textual – char and String </li></ul><ul><li>String </li></ul><ul><ul><li>• Is not a primitive data type; it is a class </li></ul></ul><ul><ul><li>• Has its literal enclosed in double quotes (&quot; &quot;) </li></ul></ul><ul><ul><li>&quot;The quick brown fox jumps over the lazy dog.&quot; </li></ul></ul><ul><ul><li>• Can be used as follows: </li></ul></ul><ul><ul><li>String greeting = &quot;Good Morning !! n&quot;; </li></ul></ul><ul><ul><li>String errorMessage = &quot;Record Not Found !&quot;; </li></ul></ul><ul><li>  </li></ul>
  11. 11. <ul><li>Integral – byte, short, int, and long </li></ul><ul><li>• Uses three forms – Decimal, octal, or hexadecimal </li></ul><ul><ul><li>2 The decimal value is two </li></ul></ul><ul><ul><li>077 The leading zero indicates an octal value </li></ul></ul><ul><ul><li>0xBAAC The leading 0x indicates a hexadecimal value </li></ul></ul><ul><li>• Has a default int </li></ul><ul><li>• Defines long by using the letter L or l </li></ul>
  12. 12. Integral – byte, short, int, and long Integral data types have the following ranges: Integer Length Name or Type Range 8 bits byte -2^7 to 2^7-1 16 bits short -2^15 to 2^15 -1 32 bits int -2^31 to 2^31 -1 64 bits long -2^63 to 2^63 -1
  13. 13. <ul><li>Floating Point – float and double </li></ul><ul><ul><li>• Default is double </li></ul></ul><ul><ul><li>• Floating point literal includes either a decimal point or </li></ul></ul><ul><ul><li>one of the following: </li></ul></ul><ul><ul><li>E or e (add exponential value) </li></ul></ul><ul><ul><li>F or f (float) </li></ul></ul><ul><ul><li>D or d (double) </li></ul></ul><ul><ul><li>3.14 A simple floating-point value (a double) </li></ul></ul><ul><ul><li>6.02E23 A large floating-point value </li></ul></ul><ul><ul><li>2.718F A simple float size value </li></ul></ul><ul><ul><li>123.4E+306D A large double value with redundant D </li></ul></ul>
  14. 14. <ul><li>Floating Point – float and double </li></ul><ul><li>• Floating point data types have the following ranges: </li></ul><ul><ul><li>Float Length Name or Type </li></ul></ul><ul><ul><li>32 bits float </li></ul></ul><ul><ul><li>64 bits double </li></ul></ul><ul><li>  </li></ul>
  15. 15. <ul><li>Variables, Declarations, and Assignments </li></ul><ul><ul><li>public class Assign { </li></ul></ul><ul><ul><ul><li>public static void main(String args []) { </li></ul></ul></ul><ul><ul><ul><li>int x, y; // declare int variables </li></ul></ul></ul><ul><ul><ul><li>float z = 3.414f; // declare and assign float </li></ul></ul></ul><ul><ul><ul><li>double w = 3.1415; // declare and assign double </li></ul></ul></ul><ul><ul><ul><li>boolean truth = true; // declare and assign boolean </li></ul></ul></ul><ul><ul><ul><li>char c; // declare character variable </li></ul></ul></ul><ul><ul><ul><li>String str; // declare String </li></ul></ul></ul><ul><ul><ul><li>String str1 = &quot;bye&quot;; // declare and assign String variable </li></ul></ul></ul><ul><ul><ul><li>c = 'A'; // assign value to char variable </li></ul></ul></ul><ul><ul><ul><li>str = &quot;Hi out there!&quot;; // assign value to String variable </li></ul></ul></ul><ul><ul><ul><li>x = 6; </li></ul></ul></ul><ul><ul><ul><li>y = 1000; // assign values to int variables </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>
  16. 16. <ul><ul><li>Java Reference Types </li></ul></ul><ul><ul><li>• Beyond primitive types all others are reference types </li></ul></ul><ul><ul><li>• A reference variable contains a &quot;handle&quot; to an object </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><ul><li>public class MyDate { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private int day = 1; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private int month = 1; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private int year = 2000; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public class TestMyDate { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public static void main(String[] args) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>MyDate my_birth = new MyDate(22, 7, 1964); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
  17. 17. <ul><li>Constructing and Initializing Objects </li></ul><ul><li>• Calling new Xxx() to allocate space for the new object </li></ul><ul><li>results in: </li></ul><ul><li>* Space for the new object is allocated and instance </li></ul><ul><li>variables are initialized to their default values </li></ul><ul><li>* Explicit attribute initialization is performed </li></ul><ul><li>* A constructor is executed </li></ul><ul><li>* Variable assignment is made to reference the object </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>MyDate my_birth = new MyDate(22, 7, 1964); </li></ul></ul>
  18. 20. Example OverloadCons.java
  19. 25. <ul><li>The this Reference </li></ul><ul><ul><li>Here are a few uses of the this keyword: </li></ul></ul><ul><ul><li>• this is used to disambiguate a local method or </li></ul></ul><ul><ul><li>constructor variable from an instance variable </li></ul></ul><ul><ul><li>• To pass the current object as a parameter to another </li></ul></ul><ul><ul><li>method or constructor </li></ul></ul>
  20. 26. <ul><li>The this Reference </li></ul><ul><ul><li>public class MyDate { </li></ul></ul><ul><ul><li>private int day = 1; </li></ul></ul><ul><ul><li>private int month = 1; </li></ul></ul><ul><ul><li>private int year = 2000; </li></ul></ul><ul><ul><li>public MyDate(int day, int month, int year) { </li></ul></ul><ul><ul><li>this.day = day; </li></ul></ul><ul><ul><li>this.month = month; </li></ul></ul><ul><ul><li>this.year = year; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  21. 27. Java Coding Conventions • Packages: package banking.domain; • Classes: The first letter should be capitalized and if several words are linked together , the first letter of inner word should be uppercase,class name should typically be noun. For example Dog, Account, PrintWriter • Interfaces: The first letter should be capitalized and if several words are linked together , the first letter of inner word should be uppercase,interface n ame should typically be adjective For example Runnable, Serializable
  22. 28. Java Coding Conventions • Methods: The first letter should be lowercase, and then normal, the name should typically be verb-noun pairs. For example getBalance, doCalculation, setCustomerName • Variables: The first letter should be lowercase, and then normal For example currentCustomer • Constants: Java constants are created by marking variables static and final. They should be named using uppercase letter with underscore as separator. HEAD_COUNT MAXIMUM_SIZE
  23. 29. Access Modifiers Modifiers are Java Keywords that give the compiler information about the nature of code ,data or classes. Access modifiers are public , private and protected .
  24. 30. <ul><ul><li>Public </li></ul></ul><ul><ul><li>Public feature is accessed by any class. </li></ul></ul><ul><ul><li>An applet(a subclass of java.applet.Applet)is declared </li></ul></ul><ul><ul><li>as public so that it may be instantiated by browser. </li></ul></ul><ul><ul><li>An application declares its main() method to be public </li></ul></ul><ul><ul><li>so that main() can be invoked from any Java runtime </li></ul></ul><ul><ul><li>environment. </li></ul></ul><ul><li>Example package p1,p2 </li></ul>
  25. 31. <ul><ul><li>Private </li></ul></ul><ul><ul><li>The least generous access modifier is private . </li></ul></ul><ul><ul><li>Private variable or method may only be used by </li></ul></ul><ul><ul><li>an instance of class that declare the variable or </li></ul></ul><ul><ul><li>method. </li></ul></ul>
  26. 32. <ul><ul><li>Default </li></ul></ul><ul><ul><li>Default is the name given to access level that results </li></ul></ul><ul><ul><li>from not specifying an access modifier. </li></ul></ul><ul><ul><li>Default means the features are accessible in the same </li></ul></ul><ul><ul><li>package </li></ul></ul><ul><ul><li>Class outside the package may not access the default </li></ul></ul><ul><ul><li>features.Classes outside the package may subclass the </li></ul></ul><ul><ul><li>classes in the package however even the subclasses </li></ul></ul><ul><ul><li>may not access the default features </li></ul></ul>
  27. 33. <ul><li>Protected </li></ul><ul><ul><li>A protected feature of class is available to all classes </li></ul></ul><ul><ul><li>in the same package , just like default. </li></ul></ul><ul><ul><li>Moverover a p rotected feature of class is available </li></ul></ul><ul><ul><li>to all the subclasses of the class that own the protected </li></ul></ul><ul><ul><li>feature. </li></ul></ul><ul><ul><li>This access is provided even to subclasses that resides </li></ul></ul><ul><ul><li>in different package from the class that owns the </li></ul></ul><ul><ul><li>protected feature. </li></ul></ul>
  28. 34. <ul><li>Final </li></ul><ul><li>Final modifiers applies to classes,method and </li></ul><ul><li>variables. </li></ul><ul><li>Final class cannot be subclassed . </li></ul><ul><li>For example </li></ul><ul><ul><li>class SubMath extends java.lang.Math { } </li></ul></ul><ul><ul><li>will give compiler error saying “cannot subclass </li></ul></ul><ul><ul><li>final class” </li></ul></ul><ul><ul><li>In practice, avoid making a final class, unless you have security </li></ul></ul><ul><ul><li>issues, as final class wipes out key benefit of OO- extensibility. </li></ul></ul><ul><li>Final variable cannot be modified once it has been </li></ul><ul><li>assigned a value </li></ul>
  29. 35. <ul><li>Note Final method may not be overridden. </li></ul><ul><li>Will the following code compile. </li></ul><ul><ul><li>Class Mammal{ </li></ul></ul><ul><ul><li>final void getAround(){ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Class Dolphin extends Mammal{ </li></ul></ul><ul><ul><li>void getAround(){ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  30. 36. <ul><li>Abstract </li></ul><ul><ul><li>Abstract modifier can be applied to classes and </li></ul></ul><ul><ul><li>methods. </li></ul></ul><ul><ul><li>A class that is abstract may not be instantiated. </li></ul></ul><ul><ul><li>Abstract class provide a way to defer the implementation </li></ul></ul><ul><ul><li>to subclass. </li></ul></ul><ul><li>  </li></ul><ul><li>abstract class animal </li></ul><ul><li> void travel() </li></ul><ul><li>  </li></ul><ul><li>class Bird class Fish class Snake </li></ul><ul><li>void travel() void travel() void travel() </li></ul>
  31. 37. <ul><li>The class is declared abstract if any of the following </li></ul><ul><li>conditions are true </li></ul><ul><ul><li>• The class has one or more abstract methods. </li></ul></ul><ul><ul><li>• The class inherits one or more abstract method </li></ul></ul><ul><ul><li>for which it doesnot provide implementation. </li></ul></ul><ul><ul><li>• The class declare that it implements an interface </li></ul></ul><ul><ul><li>but doesnot provide implementation for every method </li></ul></ul><ul><ul><li>of that interface. </li></ul></ul><ul><ul><li>• Abstract is opposite to final </li></ul></ul><ul><li>Final class may not be subclassed, an abstract </li></ul><ul><li>class must be subclassed. </li></ul>
  32. 38. Abstract class vs. Interface Interface Abstract class A class may implement A class may extend several interfaces. only one abstract class. An interface cannot An abstract class can provide any code at all provide complete code, default code Static final constants only Both instance and static constants are possible.
  33. 39. If you add a new method to If you add a new method to an an interface, you must track abstract class, you have the down all implementations option of providing a default of that interface in the universe implementation of it. Then all and provide them with a concrete existing code will continue to implementation of that method. work without change. An interface implementation The existing class must be may be added to any existing rewritten to extend only from class. the abstract class.
  34. 40. <ul><li>Static </li></ul><ul><ul><li>This can be applied to variables , methods </li></ul></ul><ul><ul><li>and a strange kind of code that is not part of method. </li></ul></ul><ul><ul><li>S tatic means belong to class rather that particular </li></ul></ul><ul><ul><li>instance of class. </li></ul></ul>
  35. 41. Example Ecstatic.java Class Ecstati c{ static{ System.out.println(“Hello”); } static int x=0; Ecstati c(){ x++; } }
  36. 42. <ul><ul><li>Reference a static variable via two ways </li></ul></ul><ul><ul><li>·        Via reference to instance of class </li></ul></ul><ul><ul><li>·        Via the class name. </li></ul></ul><ul><ul><li>Via reference to instance of class </li></ul></ul><ul><ul><li>Ecstatic e1=new Ecstatic(); </li></ul></ul><ul><ul><li>Ecstatic e2=new Ecstatic(); </li></ul></ul><ul><ul><li>e1.x=100; </li></ul></ul><ul><ul><li>e2.x=200; </li></ul></ul><ul><ul><li>reallyImportantVariable=e1.x; </li></ul></ul><ul><ul><li>//you may think its set to 100 but really its 200. </li></ul></ul>
  37. 43. Via class name Ecstatic e1=new Ecstatic(); Ecstatic e2=new Ecstatic(); Ecstatic.x=100; Ecstatic.x=200; reallyImportantVariable=Ecstatic.x; //set to 200 Note • With static methods there is no this.If a static method need to access a non static variable it must specify the instance of the class that own the variable . • S tatic method cannot be overridden to be non static. Example StaticOverride.java
  38. 44. <ul><ul><li>Native </li></ul></ul><ul><ul><li>This modifier can be applied only to methods. </li></ul></ul><ul><ul><li>Like the abstract keyword the native indicates the body </li></ul></ul><ul><ul><li>of method is to be found elsewhere. </li></ul></ul><ul><ul><li>In the case of abstract method the body lies in a subclass </li></ul></ul><ul><ul><li>W ith native methods the body lies outside the JVM in a </li></ul></ul><ul><ul><li>library. </li></ul></ul><ul><ul><li>Native code is written in Non Java language </li></ul></ul><ul><ul><li>typically C or C++. </li></ul></ul>
  39. 45. <ul><ul><li>When the native method is invoked the library that contain the native code is loaded and available to JVM, if it is not loaded there will be a delay. </li></ul></ul><ul><ul><li>Library is loaded by System.loadLibrary(“library_name”); </li></ul></ul>
  40. 46. Transient This modifier applies only to variables. The transient variable is not stored as a part of its object persistent state. Sometimes object contain extremely sensitive information class WealthyCustomer extends Customer implements Serializable{ private float $wealth; private String accessCode; }
  41. 47. <ul><li>Now if instance of this class is written to a file </li></ul><ul><li>somebody could snoop the access code. </li></ul><ul><ul><li>To avoid it write the code like this </li></ul></ul><ul><ul><li>class WealthyCustomer extends Customer implements </li></ul></ul><ul><ul><li>Serializable{ </li></ul></ul><ul><ul><li>private float $wealth; </li></ul></ul><ul><ul><li>private transient String accessCode; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Now access code will not be written out during </li></ul></ul><ul><ul><li>serialization. </li></ul></ul>
  42. 48. <ul><ul><li>Synchronized </li></ul></ul><ul><ul><li>Methods can be declared synchronized. </li></ul></ul><ul><ul><li>This is used to control access to critical code in </li></ul></ul><ul><ul><li>multithreaded program. </li></ul></ul><ul><ul><li>  </li></ul></ul><ul><ul><li>Volatile </li></ul></ul><ul><ul><li>Only variable may be volatile ,declaring a variable </li></ul></ul><ul><ul><li>volatile means it can be modified asynchronously. </li></ul></ul><ul><ul><li>For example, a variable that might be concurrently </li></ul></ul><ul><ul><li>modified by multiple threads (without locks or a </li></ul></ul><ul><ul><li>similar form of mutual exclusion) should be declared </li></ul></ul><ul><ul><li>volatile. </li></ul></ul>
  43. 49. <ul><ul><li>Strictfp </li></ul></ul><ul><ul><li>strictfp forces floating points to adhere to the IEEE 754 standard. </li></ul></ul><ul><ul><li>With strictfp, you can predict how your floating points will behave </li></ul></ul><ul><ul><li>regardless of the underlying platform the JVM is running on. </li></ul></ul><ul><li>The downside is that if the underlying platform is capable of </li></ul><ul><li>supporting greater precision, a strictfp method won't be able to take </li></ul><ul><li>advantage of it. </li></ul><ul><li>Use strictfp as a class modifier , if donot declare a class as strictfp </li></ul><ul><li>declare an individual method as strictfp. </li></ul>
  44. 50. Summary   Modifier Class Variable Method Constructor public yes yes yes yes protected no yes yes yes default yes yes yes yes private no yes yes yes final yes yes yes no abstract yes no yes no static no yes yes no native no no yes no transient no yes no no volatile no yes no no synchronized no no yes no
  45. 52. <ul><li>Which of the following declarations are illegal? </li></ul><ul><li>(Choose all that apply.) </li></ul><ul><ul><li>A. default String s; </li></ul></ul><ul><ul><li>B. transient int i = 41; </li></ul></ul><ul><ul><li>C. public final static native int w(); </li></ul></ul><ul><ul><li>D. abstract double d; </li></ul></ul><ul><ul><li>E. abstract final double hyperbolicCosine(); </li></ul></ul><ul><li>Which of the following statements is true? </li></ul><ul><ul><li>A. An abstract class may not have any final methods. </li></ul></ul><ul><ul><li>B. A final class may not have any abstract methods. </li></ul></ul>
  46. 53. <ul><ul><li>Answer : A, D, E. A is illegal because “default” is not </li></ul></ul><ul><ul><li>a keyword. B is a legal transient declaration. C is strange </li></ul></ul><ul><ul><li>but legal. D is illegal because only methods and classes </li></ul></ul><ul><ul><li>may be abstract. E is illegal because abstract and </li></ul></ul><ul><ul><li>final are contradictory. </li></ul></ul><ul><ul><li>Answer : B </li></ul></ul>
  47. 54. <ul><ul><li>Question . Which of the following statements is true? </li></ul></ul><ul><ul><li>A. Transient methods may not be overridden. </li></ul></ul><ul><ul><li>B. Transient methods must be overridden. </li></ul></ul><ul><ul><li>C. Transient classes may not be serialized. </li></ul></ul><ul><ul><li>D. Transient variables must be static. </li></ul></ul><ul><ul><li>E. Transient variables are not serialized. </li></ul></ul>
  48. 55. <ul><ul><li>Answer : E. </li></ul></ul><ul><ul><li>A, B, and C don’t mean anything, because </li></ul></ul><ul><ul><li>only variables may be transient, not methods or classes. </li></ul></ul><ul><ul><li>D is false because transient variables may never be static. </li></ul></ul><ul><ul><li>E is a good one-sentence definition of transient. </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×