java session02
TRANSCRIPT
-
8/8/2019 JAVA Session02
1/26
Slide 1 of 26Session 2Ver. 1.0
Java Programming Language
In this session, you will learn to:
Distinguish between valid and invalid identifiers
List the eight primitive types
Define literal values for numeric and textual types
Define the terms primitive variable and reference variableDeclare variables of class type
Construct an object using new
Describe default initialization
Distinguish between instance and local variables
Objectives
-
8/8/2019 JAVA Session02
2/26
Slide 2 of 26Session 2Ver. 1.0
Java Programming Language
Objectives (Contd.)
Describe how to initialize instance variables
Identify boolean expressions and their requirements in controlconstructs
Recognize assignment compatibility and required casts in
fundamental typesUse if, switch, for, while, and do constructions and the labeledforms of break and continue as flow control structures in aprogram
-
8/8/2019 JAVA Session02
3/26
Slide 3 of 26Session 2Ver. 1.0
Java Programming Language
Identifier
An identifier is a name given to a variable, class, or method.
Identifiers have the following characteristics:
Can start with a Unicode letter, underscore (_), or dollar sign($)
Are case-sensitive and have no maximum lengthExamples of valid identifiers:
identifier
userName
user_name
_sys_var1$change
-
8/8/2019 JAVA Session02
4/26
Slide 4 of 26Session 2Ver. 1.0
Java Programming Language
Basic Data Types in Java
Java programming language supports two basic data types:
Primitive types
Class types
-
8/8/2019 JAVA Session02
5/26
Slide 5 of 26Session 2Ver. 1.0
Java Programming Language
Primitive data types are simple values, not objects.
The Java programming language defines eight primitivedata types, which can be considered in four categories:
Logical boolean
Textual char
Integral byte, short, int, and long
Floating double and float
Primitive Data Types
-
8/8/2019 JAVA Session02
6/26
Slide 6 of 26Session 2Ver. 1.0
Java Programming Language
Class Data Type
Class types are used for more complex types, including allthe types that you declare yourself.
They are used to create objects.
-
8/8/2019 JAVA Session02
7/26
Slide 7 of 26Session 2Ver. 1.0
Java Programming Language
Variables, Declarations, and Assignments
Possible ways to declare and assign variables in Java areas follows:
public class Assign
{ public static void main (String args[])
{// declare and assign values to int
integer variables int x=6, y=1000;// declare and assign floating pointfloat z = 3.414f;// declare and assign boolean
boolean truth = true;// declare and assign String variableString str1 = "bye";// declare and assign value to char variablechar c = 'A'; }}
-
8/8/2019 JAVA Session02
8/26
Slide 8 of 26Session 2Ver. 1.0
Java Programming Language
Java Reference Type
In Java technology, beyond primitive types all other datatypes are reference types.
A reference variable contains a handle to an object.
For example:
public class MyDate
{ private int day = 1;
private int month = 1;
private int year = 2000;
publ
ic MyDate(int day, int mo
nth, intyear) { .... }
public String toString() { . }
}
-
8/8/2019 JAVA Session02
9/26
Slide 9 of 26Session 2Ver. 1.0
Java Programming Language
Java Reference Type (Contd.)
public class TestMyDate {
public static void main(String[] args)
{ MyDate today = new MyDate(22, 7,1964);
}
}
Variable today is a reference variable holding one object ofMyDate class.
-
8/8/2019 JAVA Session02
10/26
Slide 10 of 26Session 2Ver. 1.0
Java Programming Language
Constructing and Initializing Objects
Calling new xyz() performs the following actions:
Memory is allocated for the object
Explicit attribute initialization is performed
A constructor is executed
The object reference is returned by the new operatorThe reference to the object is assigned to a variable
For example:
MyDate my_birth = new MyDate(22, 7, 1964);
-
8/8/2019 JAVA Session02
11/26
Slide 11 of 26Session 2Ver. 1.0
Java Programming Language
Pass-By-Value
In a single Java Virtual Machine, the Java programminglanguage only passes arguments by value.
When an object instance is passed as an argument to amethod, the value of the argument is a reference to the
object.The contents of the object can be changed in the calledmethod, but the original object reference is never changed.
-
8/8/2019 JAVA Session02
12/26
Slide 12 of 26Session 2Ver. 1.0
Java Programming Language
The this Reference
Two uses of the this keyword are:
To resolve ambiguity between instance variables andparameters
To pass the current object as a parameter to another method
or constructor
-
8/8/2019 JAVA Session02
13/26
Slide 13 of 26Session 2Ver. 1.0
Java Programming Language
Lets see how to define a reference type instance variable in aJava class, and manipulate the object referenced by thisvariable.
Demonstration
-
8/8/2019 JAVA Session02
14/26
Slide 14 of 26Session 2Ver. 1.0
Java Programming Language
Java Programming Language Coding Conventions
Examples of coding conventions are:
Packages:
com.example.domain;
Classes, interfaces, and enum types:
SavingsAccount
Methods:
getAccount()
Variables:
CurrentCustomer
Constants:HEAD_COUNT
-
8/8/2019 JAVA Session02
15/26
Slide 15 of 26Session 2Ver. 1.0
Java Programming Language
Variables and Scope
Variables defined inside a method are called local variables,also referred to as automatic, temporary, or stack variables.Local variables must be initialized before the first use.
Variables defined outside a method are created when the
object is constructed using the new xxx() call. They are oftwo types:
Static variables: They are created when the class is loadedand continue to exist for as long as the class is loaded.
Instance variables: They are declared without using thestatic keyword. They continue to exist for as long as the
object exists.
-
8/8/2019 JAVA Session02
16/26
Slide 16 of 26Session 2Ver. 1.0
Java Programming Language
Logical Operators
The boolean operators are:
!NOT & AND
| OR ^ XOR
The short-circuit boolean operators are:
&& AND || OR
You can use these operators as follows:
MyDate d = reservation.getDepartureDate();
if ( (d != null) && (d.day > 31) {
// do
so
mething with d}
-
8/8/2019 JAVA Session02
17/26
Slide 17 of 26Session 2Ver. 1.0
Java Programming Language
Right-Shift Operators >> and >>>
The operator >> performs an arithmetic or signed right shift.The result of this shift is that the first operand is divided by 2raised to the number of times specified by the secondoperand.
The sign bit is copied during the shift.This operator is used for bit patterns.
The sign bit is not copied during the shift.
Examples are:
128 >> 1 returns 128/21 = 64
256 >> 4 returns 256/24 = 16
-256 >> 4 returns -256/24 = -16
-
8/8/2019 JAVA Session02
18/26
Slide 18 of 26Session 2Ver. 1.0
Java Programming Language
Left-ShiftOperator
-
8/8/2019 JAVA Session02
19/26
Slide 19 of 26Session 2Ver. 1.0
Java Programming Language
Casting
Casting means assigning a value of one type to a variable ofanother type.
If information might be lost in an assignment, theprogrammer must confirm the assignment with a cast.
The assignment between long and int requires an explicitcast.
Examples of casting are:
long bigValue = 99L;
int squashed = bigValue; // Wrong, needs a cast
int squashed = (int) bigValue; // OK
int squashed = 99L; // Wrong, needs a cast
-
8/8/2019 JAVA Session02
20/26
Slide 20 of 26Session 2Ver. 1.0
Java Programming Language
Promotion and Casting of Expressions
Variables are promoted automatically to a longer form (suchas int to long).
Expression is assignment-compatible if the variable type isat least as large (the same number of bits) as the
expression type.For Example:
long bigval = 6; // 6 is an int type, OK
int smallval = 99L; // 99L is a long, illegal
double z = 12.414F; // 12.414F is float, OK
float z1 = 12.414; // 12.414 is double, illegal
-
8/8/2019 JAVA Session02
21/26
Slide 21 of 26Session 2Ver. 1.0
Java Programming Language
Branching Statement
Conditional statements enable the selective execution ofportions of the program according to the value of someexpressions.
Simple if, else Statements:
The syntax is:if ( )
Complex if, else Statements:
The syntax is:
if ( )
else
-
8/8/2019 JAVA Session02
22/26
Slide 22 of 26Session 2Ver. 1.0
Java Programming Language
The switch Statement
The switch statement:
The syntax is:switch ( ) {
case :
*[break;]
case :
*
[break;]
default:
*[break;]}
In the switch () statement, must be expression compatible with anint type.
-
8/8/2019 JAVA Session02
23/26
Slide 23 of 26Session 2Ver. 1.0
Java Programming Language
Looping Statement
Looping Statements enable you to execute block ofstatements repeatedly.
Java programming language supports three types of loopconstructs:
The for loopThe while loop
The do/while loop
-
8/8/2019 JAVA Session02
24/26
Slide 24 of 26Session 2Ver. 1.0
Java Programming Language
Special Loop Flow Control
The following statements can be used to further control loopstatements:
The break []; command
The continue []; command
The : command, where should be a loop
-
8/8/2019 JAVA Session02
25/26
Slide 25 of 26Session 2Ver. 1.0
Java Programming Language
Summary
In this session, you learned that:
An identifier is a name given to a variable, class, or method. Anidentifier can not be a keyword.
Java technology keywords identify a data type name or program
construct name.The Java programming language provides class andprimitive data types.
An object can be constructed using the keyword new.
The Java programming language passes arguments only byvalue.
The this keyword is used to resolve ambiguity betweeninstance variables and parameters.
There are two types of variables, primitive type and referencetype.
-
8/8/2019 JAVA Session02
26/26
Slide 26 of 26Session 2Ver. 1.0
Java Programming Language
Summary (Contd.)
Casting means assigning a value of one type to a variable ofanother type.
Conditional statements enable the selective execution ofportions of the program according to the value of someexpressions:
If and switch statements help to perform two-way and
multiple-way branching.
The for and while loops test the loop condition before
executing the loop body.
The do loops check the loop condition after executing the loop
body.
The labeled forms ofbreak and continue help to furthercontrol the loop statements.