Unformatted text preview:

Additional Java Syntax Jan 14 2019 Odd corners We have already covered all of the commonly used Java syntax Some Java features are seldom used because You should be at least aware of these features because They are needed in only a few specialized situations or It s just as easy to do without them or Few people know about them You may encounter them in someone else s code They may do something you will need someday There are also a few useful features that we just didn t get around to or didn t discuss in sufficient detail Reserved words A reserved word or keyword is one that has a special meaning and you may not use it as a name There are also important keywords that we have not talked about Examples if while class package native strictfp transient volatile And there are keywords that are not used but you still can t use them as names const goto Unused keywords const is used in some languages to declare constants Java has this keyword but used final instead A final variable does not have to be given a value when it is assigned but it can be given a value only once final int lesser if x y lesser x else lesser y legal The keyword final is also used to indicate that A class cannot be subclassed A method cannot be overridden It does not indicate that a variable cannot be shadowed Some languages allow you to put a label on a statement and to goto jump to that statement from elsewhere in the program Java reserves the goto keyword but does not use it native Sometimes you may wish to use a method that is defined in some other language such as C or C The method modifier native tells Java to link to this native method To do this you need to look into the JNI Java Native Interface API Drawbacks You lose platform independence For security reasons you cannot use native code in an applet strictfp While Java is extremely platform independent it isn t perfect There is a standard IEEE 754 that specifies exactly how floatingpoint arithmetic should be carried out It is computationally much less efficient to meet this standard if the hardware does not already meet it If this degree of platform independence is required you can use strictfp in front of any class interface or method declaration Due to differing machine implementations of floating point arithmetic floating point results may be off by a couple of the least significant bits A constructor cannot be declared FP strict to do this you should designate the entire class as FP strict A method in an interface cannot be declared FP strict because that s implementation information not an interface property native methods cannot be declared FP strict Every compile time constant expression is automatically FP strict transient You can serialize an object turn it into a linear sequence of bytes to write it to an output stream then de serialize it to read it in again Every field in the object must itself be Serializable If an object has fields that are not Serializable and you don t care that much about them anyway you can declare them as transient transient variables don t get serialized or de serialized volatile When the compiler optimizes your program it may keep a variable in a register for several steps in a computation If another Thread might modify that variable and you want to always get the current value as set by some other Thread of that variable you can mark it as volatile Local variables never need to be marked volatile The for loop The form of a for loop is for initialization condition step body The initialization may be a declaration or expression or a comma separated list of expressions Recall that is an operator so var expr is an expression The step may be an expression or a comma separated list of expressions The initialization condition and step may be empty but the semicolons may not be omitted An empty condition is equivalent to true Hence for is an infinite loop Example for i 0 j a length j 0 i j swap a i j synchronized You need synchronized whenever you may have multiple Threads accessing the same object at the same time A Thread gets a lock on a synchronized object before it uses it and releases the lock so another Thread can use the data when it is done You can synchronize various ways synchronized object statement synchronized instanceMethod parameters body is equivalent to instanceMethod parameters synchronized this body synchronized staticMethod parameters body is equivalent to staticMethod parameters synchronized class body Constructors and initializers cannot be synchronized Non synchronized methods can execute at the same time as synchronized methods on the same object Labeled statements A labeled statement has the syntax label statement A break statement is only legal within a loop or switch statement and has one of the forms break exit innermost enclosing loop or switch break label exit labeled enclosing loop or switch A continue statement is only legal within a loop and has one of the forms The label has the same syntax as a variable name The scope of the label is the statement continue resume from test of innermost loop continue label resume from test of labeled loop Any statement may be labeled but it only makes sense on a loop or switch statement Array initialization and literals Array initialization and array literals are very convenient and are less well known than they ought to be Initialization examples int primes 2 3 5 7 11 13 19 String languages Java C C int game 3 7 5 4 Examples of array literals myPrintArray new int 2 3 5 7 11 int foo foo new int 42 83 Person people new Person Alice new Person Bob new Person Carla new Person Don Initialization blocks The fields of an object may be initialized when they are declared or in a constructor or both They may also be declared in an initialization block which can contain statements as well as declarations class Example int x 5 done before constructor int y Example int n x y n class Example int f 1 for int i 2 i 10 i f f i Instance initialization blocks are executed when an object is created They are executed in the order in which they occur Static initialization blocks static initialization blocks are like ordinary initialization blocks except that they are executed once only when the class is first loaded Example class UsesRandomNumbers static double values new double 100 static for int i 0 i values length i values i Math random Expression statements An expression statement is an expression followed by a semicolon Only certain expressions may be used this way


View Full Document

Penn CIT 591 - Additional Java Syntax

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view Additional Java Syntax and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Additional Java Syntax and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?