session 1- language fundamentals
TRANSCRIPT
-
7/31/2019 Session 1- Language Fundamentals
1/30
Core Java Session 1- LanguageFundamentals
1/30
Java Certification
-
7/31/2019 Session 1- Language Fundamentals
2/30
Core Java Session 1- LanguageFundamentals
2/30
Introduction to Java: J2SE
-
7/31/2019 Session 1- Language Fundamentals
3/30
Core Java Session 1- LanguageFundamentals
3/30
Session 1Language Fundamentals
Operators and Assignments
(Chapter 1+2 in [Complete Java 2 Certification StudyGuide, 5th Edition] text book)
-
7/31/2019 Session 1- Language Fundamentals
4/30
Core Java Session 1- LanguageFundamentals
4/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
-
7/31/2019 Session 1- Language Fundamentals
5/30
Core Java Session 1- LanguageFundamentals
5/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
-
7/31/2019 Session 1- Language Fundamentals
6/30
Core Java Session 1- LanguageFundamentals
6/30
Source Files
Must end with the .java extension
If a public class is present, the class nameshould match the unextended filename.
Three top-level elements known ascompilation units. If they are present, thenthey must appear in the following order:
1.Package declaration2.Import statements
3.Class, interface, and enum definitions
-
7/31/2019 Session 1- Language Fundamentals
7/30
Core Java Session 1- LanguageFundamentals
7/30
Keywords
Java Keywords and Reserved Words
-
7/31/2019 Session 1- Language Fundamentals
8/30
Core Java Session 1- LanguageFundamentals
8/30
Identifiers
An identifieris 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.
-
7/31/2019 Session 1- Language Fundamentals
9/30
Core Java Session 1- LanguageFundamentals
9/30
Primitive Data Types
Aprimitive is a simple non-object data type thatrepresents a single value. Javas primitive data typesare.
boolean
char
byte
short
int
long
float
double
-
7/31/2019 Session 1- Language Fundamentals
10/30
Core Java Session 1- LanguageFundamentals
10/30
Literals
A literalis a value specified in the program source, asopposed to one determined at runtime.
Literals can represent primitive or string variables
and may appear on the right side of assignments orin method calls. You cannot assign values into literals,so they cannot appear on the left side ofassignments.
For example:String s = Characters in strings are 16-bit Unicode.;Char c=w;
-
7/31/2019 Session 1- Language Fundamentals
11/30
Core Java Session 1- LanguageFundamentals
11/30
Arrays (1)
A Java arrayis 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
-
7/31/2019 Session 1- Language Fundamentals
12/30
Core Java Session 1- LanguageFundamentals
12/30
Arrays (2)
type[] identifier; //declaration
identifier=new type[size]; //construction
-
7/31/2019 Session 1- Language Fundamentals
13/30
Core Java Session 1- LanguageFundamentals
13/30
Multi-dimension Arrays
int[][] myInts = new int[3][4];
-
7/31/2019 Session 1- Language Fundamentals
14/30
Core Java Session 1- LanguageFundamentals
14/30
Importing
Import statement brings name into thesource files namespace. (A namespace is a kindof placenot a physical place, but an abstract place
such as a directory or a source file
that containsitems with unique names).
Syntax:
import packageName.*;
or
import packageName.ClassName;
-
7/31/2019 Session 1- Language Fundamentals
15/30
Core Java Session 1- LanguageFundamentals
15/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;
-
7/31/2019 Session 1- Language Fundamentals
16/30
Core Java Session 1- LanguageFundamentals
16/30
Class Fundamentals(1)
Class Paths
When the Java compiler or the VirtualMachine needs a classfile, it searches all the
locations listed in its classpath. The classpathis formed by merging:
CLASSPATH environment variable and
-classpath or -cp command line arguments. The members of a classpath may be
directories or jar files.
-
7/31/2019 Session 1- Language Fundamentals
17/30
Core Java Session 1- LanguageFundamentals
17/30
Class Fundamentals(2)
The main() Method
The main() method is the entry point for standaloneJava applications.
The signature for main() ispublic static void main(String[] args)
args is that the user might have entered on thecommand line.
For example, consider the following command line:java Mapper France Belgium
-
7/31/2019 Session 1- Language Fundamentals
18/30
Core Java Session 1- LanguageFundamentals
18/30
Class Fundamentals(3)
Variables and Initialization
Java supports variables of three different lifetimes:
Member variable A member variable of a class is createdwhen 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 duringexecution 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 theclass is unloaded.
-
7/31/2019 Session 1- Language Fundamentals
19/30
Core Java Session 1- LanguageFundamentals
19/30
Argument Passing: By Reference
or by Value When Java passes an argument into a method
call, a copyof the argument is actually passed.
This is also true when the argument to bepassed is an object rather than a primitive.
Why? !!!
-
7/31/2019 Session 1- Language Fundamentals
20/30
Core Java Session 1- LanguageFundamentals
20/30
Object Reference
Java programs do not deal directly withobjects. When an object is constructed, theconstructor returns a valuea bit pattern
that uniquely identifies the object. This valueis known as a reference to the object.
For example:
Student s;
s = new Student(11078,John);
-
7/31/2019 Session 1- Language Fundamentals
21/30
Core Java Session 1- LanguageFundamentals
21/30
How to Create a Reference to a
Primitive?
Simply pass an array of one primitive element
over the method call.
-
7/31/2019 Session 1- Language Fundamentals
22/30
Core Java Session 1- LanguageFundamentals
22/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 thathas 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 havea longer lifetime.
-
7/31/2019 Session 1- Language Fundamentals
23/30
Core Java Session 1- LanguageFundamentals
23/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 thatis allocated and is able to determine whether that
memory is still useable. How?
Out of scope
or
Assign to null
-
7/31/2019 Session 1- Language Fundamentals
24/30
Core Java Session 1- LanguageFundamentals
24/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
-
7/31/2019 Session 1- Language Fundamentals
25/30
Core Java Session 1- LanguageFundamentals
25/30
Overview of the Java Operators
Operators in Java, in Descending Order of Precedence
Shifts bits of op1 right bydistance op2; fills with 0
bits on the left side
-
7/31/2019 Session 1- Language Fundamentals
26/30
Core Java Session 1- LanguageFundamentals
26/30
Operators in Java, in Descending Order of Precedence ...
if (x) {a = b;
}
else {a = c;}Replace with:a=x?b:c;
-
7/31/2019 Session 1- Language Fundamentals
27/30
Core Java Session 1- LanguageFundamentals
27/30
Evaluation Order
In Java, the order of evaluation of operands in anexpression 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 touse redundant bracketing to make it clear how any particularexpression should be evaluated.
-
7/31/2019 Session 1- Language Fundamentals
28/30
Core Java Session 1- LanguageFundamentals
28/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
-
7/31/2019 Session 1- Language Fundamentals
29/30
Core Java Session 1- LanguageFundamentals
29/30
Comparison Operators
Primitive types: == and !=
Reference types (object): equals() method
-
7/31/2019 Session 1- Language Fundamentals
30/30
Core Java Session 1- LanguageF d t l
30/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
Q&A
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