Anda di halaman 1dari 30

Java Certification

Core Java Session 1- Language Fundamentals

1/30

Introduction to Java: J2SE

Core Java Session 1- Language Fundamentals

2/30

Session 1

Language Fundamentals Operators and Assignments


(Chapter 1+2 in [Complete Java 2 Certification Study
Guide, 5th Edition] text book)

Core Java Session 1- Language Fundamentals

3/30

Objectives (1)
Language Fundamentals
Source Files Keywords and Identifiers Primitive Data Types Literals Arrays Importing Class Fundamentals Argument Passing: By Reference or by Value Garbage Collection
Core Java Session 1- Language Fundamentals 4/30

Objectives (2)
Operators and Assignments
Overview of the Java Operators Evaluation Order The Unary Operators The Arithmetic Operators The Comparison Operators The Bitwise Operators The Short-Circuit Logical Operators The Conditional Operator The Assignment Operators
Core Java Session 1- Language Fundamentals 5/30

Source Files
Must end with the .java extension If a public class is present, the class name should match the unextended filename. Three top-level elements known as compilation units. If they are present, then they must appear in the following order:
1.Package declaration 2.Import statements 3.Class, interface, and enum definitions
Core Java Session 1- Language Fundamentals 6/30

Keywords
Java Keywords and Reserved Words

Core Java Session 1- Language Fundamentals

7/30

Identifiers
An identifier is a word used by a programmer to name a variable, method, class, or label. An identifier must begin with a letter, a dollar sign ($), or an underscore (_); subsequent characters may be letters, dollar signs, underscores, or digits.

Core Java Session 1- Language Fundamentals

8/30

Primitive Data Types


A primitive is a simple non-object data type that represents a single value. Javas primitive data types are.
boolean char byte short int long float double
Core Java Session 1- Language Fundamentals 9/30

Literals
A literal is a value specified in the program source, as opposed to one determined at runtime. Literals can represent primitive or string variables and may appear on the right side of assignments or in method calls. You cannot assign values into literals, so they cannot appear on the left side of assignments. For example:
String s = Characters in strings are 16-bit Unicode.; Char c=w;
Core Java Session 1- Language Fundamentals

10/30

Arrays (1)
A Java array is an ordered collection of primitives, object references, or other arrays. Java arrays are homogeneous: except as allowed by polymorphism. To create and use an array, you must follow three steps:
1. Declaration 2. Construction 3. Initialization
Core Java Session 1- Language Fundamentals

11/30

Arrays (2)
type[] identifier; //declaration identifier=new type[size]; //construction

Core Java Session 1- Language Fundamentals

12/30

Multi-dimension Arrays
int[][] myInts = new int[3][4];

Core Java Session 1- Language Fundamentals

13/30

Importing
Import statement brings name into the source files namespace. (A namespace is a kind
of placenot a physical place, but an abstract place such as a directory or a source filethat contains items with unique names).

Syntax:
import packageName.*; or import packageName.ClassName;
Core Java Session 1- Language Fundamentals 14/30

static import
Without static imports, you have to do the following:
import java.awt.Color; myColor = Color.GREEN;

With a static import, you can import the name GREEN into your namespace:
import static java.awt.Color.GREEN; myColor = GREEN;
Core Java Session 1- Language Fundamentals

15/30

Class Fundamentals(1)
Class Paths When the Java compiler or the Virtual Machine needs a classfile, it searches all the locations listed in its classpath. The classpath is formed by merging:
CLASSPATH environment variable and -classpath or -cp command line arguments.

The members of a classpath may be directories or jar files.


Core Java Session 1- Language Fundamentals 16/30

Class Fundamentals(2)
The main() Method The main() method is the entry point for standalone Java applications. The signature for main() is public static void main(String[] args) args is that the user might have entered on the command line. For example, consider the following command line: java Mapper France Belgium
Core Java Session 1- Language Fundamentals 17/30

Class Fundamentals(3)
Variables and Initialization Java supports variables of three different lifetimes: Member variable A member variable of a class is created when an instance is created, and it is destroyed when the object is destroyed. Automatic variable An automatic variable of a method is created on entry to the method and exists only during execution of the method. Class variable A class variable (also known as a static variable) is created when the class is loaded and is destroyed when the class is unloaded.

Core Java Session 1- Language Fundamentals

18/30

Argument Passing: By Reference or by Value


When Java passes an argument into a method call, a copy of the argument is actually passed. This is also true when the argument to be passed is an object rather than a primitive. Why? !!!

Core Java Session 1- Language Fundamentals

19/30

Object Reference
Java programs do not deal directly with objects. When an object is constructed, the constructor returns a valuea bit pattern that uniquely identifies the object. This value is known as a reference to the object. For example:
Student s; s = new Student(11078,John);

Core Java Session 1- Language Fundamentals

20/30

How to Create a Reference to a Primitive?


Simply pass an array of one primitive element over the method call.

Core Java Session 1- Language Fundamentals

21/30

Garbage Collection (1)


Most modern languages permit you to allocate data storage during a program run. In Java, this is done directly when you create an object with the new operation and indirectly when you call a method that has local variables or arguments. Method locals and arguments are allocated space on the stack and are discarded when the method exits, but objects are allocated space on the heap and have a longer lifetime.
Core Java Session 1- Language Fundamentals

22/30

Garbage Collection (2)


In Java, you never explicitly free memory that you have allocated; instead, Java provides automatic garbage collection. The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. How?
Out of scope or Assign to null
Core Java Session 1- Language Fundamentals 23/30

Operators and Assignments


Overview of the Java Operators Evaluation Order The Unary Operators The Arithmetic Operators The Comparison Operators The Bitwise Operators The Short-Circuit Logical Operators The Conditional Operator The Assignment Operators
Core Java Session 1- Language Fundamentals 24/30

Overview of the Java Operators


Operators in Java, in Descending Order of Precedence

Shifts bits of op1 right by distance op2; fills with 0 bits on the left side

Core Java Session 1- Language Fundamentals

25/30

Operators in Java, in Descending Order of Precedence ...


if (x) { a = b; } else { a = c; } Replace with: a=x?b:c;

Core Java Session 1- Language Fundamentals

26/30

Evaluation Order
In Java, the order of evaluation of operands in an expression is fixed. Consider this code fragment:
1. int [] a = { 4, 4 }; 2. int b = 1; 3. a[b] = b = 0

It is generally better style to keep expressions simple and to use redundant bracketing to make it clear how any particular expression should be evaluated.
Core Java Session 1- Language Fundamentals 27/30

The Arithmetic Operators Problems


Arithmetic Error Conditions Integer division by zero Arithmetical incorrect result NaN (Float.NaN, Double.NaN):non-ordinal for comparisons. Overflow/Underflow

Core Java Session 1- Language Fundamentals

28/30

Comparison Operators
Primitive types: == and != Reference types (object): equals() method

Core Java Session 1- Language Fundamentals

29/30

Summary
Language Fundamentals Source Files Keywords and Identifiers Primitive Data Types Literals Arrays Importing Class Fundamentals Argument Passing: By Reference or by Value Garbage Collection Operators and Assignments
Overview of the Java Operators Evaluation Order The Unary Operators The Arithmetic Operators The Comparison Operators The Bitwise Operators The Short-Circuit Logical Operators The Conditional Operator The Assignment Operators

Q&A
Core Java Session 1- Language Fundamentals 30/30