You are on page 1of 26

Java Programming Language

Objectives

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 variable
Declare variables of class type
Construct an object using new
Describe default initialization
Distinguish between instance and local variables

Ver. 1.0 Session 2 Slide 1 of 26


Java Programming Language
Objectives (Contd.)

Describe how to initialize instance variables


Identify boolean expressions and their requirements in control
constructs
Recognize assignment compatibility and required casts in
fundamental types
Use if, switch, for, while, and do constructions and the labeled
forms of break and continue as flow control structures in a
program

Ver. 1.0 Session 2 Slide 2 of 26


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 length
Examples of valid identifiers:
identifier
userName
user_name
_sys_var1
$change

Ver. 1.0 Session 2 Slide 3 of 26


Java Programming Language
Basic Data Types in Java

Java programming language supports two basic data types:


Primitive types
Class types

Ver. 1.0 Session 2 Slide 4 of 26


Java Programming Language
Primitive Data Types

Primitive data types are simple values, not objects.


The Java programming language defines eight primitive
data types, which can be considered in four categories:
Logical – boolean
Textual – char
Integral – byte, short, int, and long
Floating – double and float

Ver. 1.0 Session 2 Slide 5 of 26


Java Programming Language
Class Data Type

Class types are used for more complex types, including all
the types that you declare yourself.
They are used to create objects.

Ver. 1.0 Session 2 Slide 6 of 26


Java Programming Language
Variables, Declarations, and Assignments

Possible ways to declare and assign variables in Java are


as 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 point
float z = 3.414f;
// declare and assign boolean
boolean truth = true;
// declare and assign String variable
String str1 = "bye";
// declare and assign value to char variable
char c = 'A'; }}

Ver. 1.0 Session 2 Slide 7 of 26


Java Programming Language
Java Reference Type

In Java technology, beyond primitive types all other data


types 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;
public MyDate(int day, int month, int
year) { …….... }
public String toString() { ………. }
}

Ver. 1.0 Session 2 Slide 8 of 26


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 of
MyDate class.

Ver. 1.0 Session 2 Slide 9 of 26


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 operator
The reference to the object is assigned to a variable
For example:
MyDate my_birth = new MyDate(22, 7, 1964);

Ver. 1.0 Session 2 Slide 10 of 26


Java Programming Language
Pass-By-Value

In a single Java Virtual Machine, the Java programming


language only passes arguments by value.
When an object instance is passed as an argument to a
method, the value of the argument is a reference to the
object.
The contents of the object can be changed in the called
method, but the original object reference is never changed.

Ver. 1.0 Session 2 Slide 11 of 26


Java Programming Language
The this Reference

Two uses of the this keyword are:


To resolve ambiguity between instance variables and
parameters
To pass the current object as a parameter to another method
or constructor

Ver. 1.0 Session 2 Slide 12 of 26


Java Programming Language
Demonstration

Lets see how to define a reference type instance variable in a


Java class, and manipulate the object referenced by this
variable.

Ver. 1.0 Session 2 Slide 13 of 26


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

Ver. 1.0 Session 2 Slide 14 of 26


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 of
two types:
Static variables: They are created when the class is loaded
and continue to exist for as long as the class is loaded.
Instance variables: They are declared without using the
static keyword. They continue to exist for as long as the
object exists.

Ver. 1.0 Session 2 Slide 15 of 26


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 something with d
}

Ver. 1.0 Session 2 Slide 16 of 26


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 2
raised to the number of times specified by the second
operand.
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

Ver. 1.0 Session 2 Slide 17 of 26


Java Programming Language
Left-Shift Operator <<

The operator << performs a left shift. The result of this shift
is that the first operand is multiplied by 2 raised to the
number specified by the second operand.
For Example:
128 << 1 returns 128 * 21 = 256
16 << 2 returns 16 * 22 = 64

Ver. 1.0 Session 2 Slide 18 of 26


Java Programming Language
Casting

Casting means assigning a value of one type to a variable


of another type.
If information might be lost in an assignment, the
programmer must confirm the assignment with a cast.
The assignment between long and int requires an explicit
cast.
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

Ver. 1.0 Session 2 Slide 19 of 26


Java Programming Language
Promotion and Casting of Expressions

Variables are promoted automatically to a longer form (such


as int to long).
Expression is assignment-compatible if the variable type is
at 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

Ver. 1.0 Session 2 Slide 20 of 26


Java Programming Language
Branching Statement

Conditional statements enable the selective execution of


portions of the program according to the value of some
expressions.
Simple if, else Statements:
The syntax is:
if ( <boolean_expression> )
<statement_or_block>
Complex if, else Statements:
The syntax is:
if ( <boolean_expression> )
<statement_or_block>
else
<statement_or_block>

Ver. 1.0 Session 2 Slide 21 of 26


Java Programming Language
The switch Statement

The switch statement:


The syntax is:
switch ( <expression> ) {
case <constant1>:
<statement_or_block>*
[break;]
case <constant2>:
<statement_or_block>*
[break;]
default:
<statement_or_block>*
[break;]}
In the switch (<expression>) statement,
<expression> must be expression compatible with an int
type.

Ver. 1.0 Session 2 Slide 22 of 26


Java Programming Language
Looping Statement

Looping Statements enable you to execute block of


statements repeatedly.
Java programming language supports three types of loop
constructs:
The for loop
The while loop
The do/while loop

Ver. 1.0 Session 2 Slide 23 of 26


Java Programming Language
Special Loop Flow Control

The following statements can be used to further control loop


statements:
The break [<label>]; command
The continue [<label>]; command
The <label> : <statement> command, where
<statement> should be a loop

Ver. 1.0 Session 2 Slide 24 of 26


Java Programming Language
Summary

In this session, you learned that:


An identifier is a name given to a variable, class, or method. An
identifier can not be a keyword.
Java technology keywords identify a data type name or program
construct name.
The Java programming language provides class and
primitive data types.
An object can be constructed using the keyword new.
The Java programming language passes arguments only by
value.
The this keyword is used to resolve ambiguity between
instance variables and parameters.
There are two types of variables, primitive type and reference
type.

Ver. 1.0 Session 2 Slide 25 of 26


Java Programming Language
Summary (Contd.)

Casting means assigning a value of one type to a variable of


another type.
Conditional statements enable the selective execution of
portions of the program according to the value of some
expressions:
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 of break and continue help to further
control the loop statements.

Ver. 1.0 Session 2 Slide 26 of 26

You might also like