Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Session02-Learning The Java Language

The document provides an overview of learning the Java language, covering topics such as data types, variables, arrays, operators, control flow statements, strings, type conversions, input/output, and code style. It includes examples and explanations of key Java concepts.

Uploaded by

Hoàng Long
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Session02-Learning The Java Language

The document provides an overview of learning the Java language, covering topics such as data types, variables, arrays, operators, control flow statements, strings, type conversions, input/output, and code style. It includes examples and explanations of key Java concepts.

Uploaded by

Hoàng Long
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Session 02

Learning the Java Language

(http://docs.oracle.com/javase/tutorial/java/index.html)

Session 02 - Learning the Java Language


Objectives

• Study some fundamentals of Java


languages: Data types, variables, arrays,
operators, logic constructs.
• Pass arguments to the main method
• Input/output variables
• Object-Oriented Programming Concepts:
Class, Interface, Package.

Session 02 - Learning the Java Language


Keywords and Identifiers
 Keywords: Almost of them are similar to
those in C language
 Naming Convention:

Letter Letters
$ Digits, $
_ _

• Java is a case-sensitive language


• Identifiers must be different to keywords

3/40
Primitive Data Types - Variables
Type Bytes Minimum Maximum
• A primitive is a
simple non- char 2 \u0000 \uFFFF
object data byte 1 -27 27 - 1
type that short 2 -215 215 – 1
represents a int 4 -231 231 – 1
single value.
Java’s long 8 -263 263 - 1
primitive data float 4
types are: double 8
boolean true/false

Type var [=Initial value] ;


Session 02 - Java Fundamentals
Operators
Category Operators
(Descending Precedence)

Unary ++ -- + - ! ~ (type)
Arithmetic * / %
+ -
Shift << >> >>>
Comparison < <= > >= instanceof
== !=
Bitwise & ^ |
They are the same with
Short-circuit && ||
those in C language
Conditional ?:
Assignment = op=
Session 02 - Learning the Java Language
Using Operators Demonstration

6/40
Using Operators Demonstration
Use 2 bytes to store value
1:  0000 0000 0000 0001
1111 1111 1111 1110 ( 1-complement)
-1  1111 1111 1111 1111 ( 2-complement)
-1 <<1  1111 1111 1111 1110 (-2)

-1  1111 1111 1111 1111


-1 >>1  1111 1111 1111 1111

-1  1111 1111 1111 1111


3 0000 0000 0000 0011 -1 >>>1  0111 1111 1111 1111 (2147483647)
4 0000 0000 0000 0100
3|4  0000 0000 0000 0111 (7)
3 0000 0000 0000 0011
4 0000 0000 0000 0100
3&4  0000 0000 0000 0000 (0)
3 0000 0000 0000 0011
4 0000 0000 0000 0100
3^4  0000 0000 0000 0111 (7 ): XOR BIT
7/40
Literals and Value Variables
 Character: ‘a’ Value variable

 String: String S=“Hello”;


 Escape sequences: see the page
10
Stack
 Integral literals:
28, 0x1c, 0X1A ( default: int).
123l, 123L (long)
 Floating point:
1.234 (default: double) n 10

1.3f 1.3F
1.3E+21 int n=10;
1.3d 1.3D
8/40
Java Expressions
• Java is an expression-oriented language. A
simple expression in Java is either:
• A constant: 7, false
• A char - literal enclosed in single quotes: 'A', '3‘
• A String - literal enclosed in double quotes: "foo“
• The name of any properly declared variables: x
• Any two|one of the preceding types of
expression that are combined with one of the
Java binary operators: i++, x + 2, (x + 2)

Session 02 - Java Fundamentals


One Dimensional Arrays (1)

• An array is a container object that holds a


fixed number of values of a single type.
• The length of an array is established when
the array is created.
• Each item in an array is called an element,
and each element is accessed by its
numerical index.

Session 02 - Java Fundamentals


One Dimensional Arrays (2)

• Declaring a Variable to Refer to an Array


int[] anArray;
or float anArrayOfFloats[];
• Creating, Initializing, and Accessing an
Array
anArray = new int[10];
• Copying Arrays
• Use arraycopy method from System class.
Session 02 - Java Fundamentals
One Dimensional Arrays (3)
int[] ar;
ar= new int[3];
3
ar[0]=1; ar[1]=2; ar[2]=3; 2
Heap
int a2[]; 10000 1

int[] a3 = {1,2,3,4,5};
int a4[] = {1,2,3,4,5};
Stack ar 10000

Array is a reference variable


int n=10;

12/40
Multiple Dimensional Arrays
10 2002

9 500
2001
92
500
8 91
200 200
7
100 4
6 8000

5 3
1000
8000 2
1000 m
replacement 1
100

int m[][]= { {1,2,3,4}, {91,92}, {2001,2002}};


int[] replacement = {5,6,7,8,9,10}; m[i][j]
m[1]= replacement; int[][] m; // declare a matrix
int r=10, c=5; // number of rows, columns
m= new int[r][c]; // memory allocate
13/40
Evaluating Expressions and
Operator Precedence
• The compiler generally evaluates such
expressions from the innermost to outermost
parentheses, left to right.
int x = 1; int y = 2; int z = 3;
int answer = ((8 * (y + z)) + y) * x;
would be evaluated piece by piece as follows:
((8 * (y + z) ) + y) * x
((8 * 5) + y) * x
(40 + y) * x
42 * x
42
Session 02 - Java Fundamentals
Operator Precedence- Evaluation Order

Order:
(1) [ ]  a[b]  a[1]
(2) = ( from the right)  b=0  return 0
 a[1] = 0

15/40
Basic Constructs

 They are taken from C-language


 Selection
if, if … else
switch (char/int exp)… case … default…
 Loops
for
do… while
while

Session 02 - Learning the Java Language


Basic Logic Constructs
 They are the same with those in C-statements

An enhanced for loop

a 1 2 3 4 5

x 1

17/40
The String type

• A String represents a sequence of zero or


more Unicode characters.
• String name = "Steve";
• String s = “”;
• String s = null;
• String concatenation.
• String x = "foo“ + "bar“ + "!";
• Java is a case-sensitive language.

Session 02 - Java Fundamentals


Type Conversions and Explicit Casting

* Widening Conversion: OK
• Narrowing conversion: Not
allowed. We must use
explicit casting.
• A boolean can not be
converted to any other
type.
• A non-boolean can be
converted to another non-
boolean type.

0000 0001
0000 0000

y n
19/34
Scope of a Variable

Scope of the
variable y

Scope of the
variable i

Session 02 - Java Fundamentals


Input/Output Data

 Class java.lang.System
 Class java.util.Scanner
Refer to Java documentation:
java.lang.String class,
- the format method,
- format string
for more details

n= sc.nextInt();
Session 02 - Java Fundamentals
Elements of Java Style

• Proper Use of Indentation


• Statements within a block of code should be indented
relative to the starting/ending line of the enclosing
block.
• Use Comments Wisely
• Placement of Braces
• Opening brace at the end of the line of code that
starts a given block. Each closing brace goes on its
own line, aligned with the first character of the line
con.
• Descriptive Variable Names
Session 02 - Java Fundamentals
Pass Arguments to the method main

Session 02 - Learning the Java Language


Pass
Arguments to
the method
main

Session 02 - Learning the Java Language


What Is an
Object?(1)

• Objects are key to understanding object-


oriented technology.
• Examples of real-world objects: your dog,
your desk, your television set, your bicycle.
• Real-world objects share two characteristics:
They all have state and behavior.
• Dogs have state (name, color, breed, hungry)
and behavior (barking, fetching, wagging tail).
Session 02 - Learning the Java Language
What Is an Object?(2)
• Software objects are conceptually similar
to real-world objects: they too consist of
state and related behavior.
• An object stores its state in fields and
exposes its behavior through methods.

Session 02 - Learning the Java Language


What Is an Object?(3)
• Software objects provides a number of
benefits:
• Modularity
• Information-hiding
• Code re-use
• Pluggability and debugging ease

Session 02 - Learning the Java Language


What Is a Class?
• A class is the blueprint
from which individual
objects are created.
• Your bicycle is
an instance of the class of
objects known as bicycles.

Session 02 - Learning the Java Language


What Is Inheritance?
• Object-oriented programming allows
classes to inherit commonly used state
and behavior from other classes.
• Bicycle now becomes the superclass of
MountainBike, RoadBike, and TandemBike

class MountainBike
extends Bicycle {
// new fields and methods defining
// a mountain bike would go here
}

Session 02 - Learning the Java Language


What Is an Interface?
• An interface is a group of related methods
with empty bodies.
interface Bicycle {
/ wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}

Session 02 - Learning the Java Language


What Is a Package?
• A package is a namespace that organizes
a set of related classes and interfaces.
• The Java platform provides an enormous
class library (a set of packages) suitable
for use in your own applications called
API.
• For example, a String object contains state and
behavior for character strings.

Session 02 - Learning the Java Language


User-Defined Package
• Add a Java
class

If package is used, it must be


the first line in Java code

Session 02 - Learning the Java Language


User-Defined Package

Session 02 - Learning the Java Language


Summary
• The core concepts behind object-oriented
programming: objects, interfaces, classes,
and inheritance.
• The traditional features of the language,
including variables, arrays, data types,
operators, and control flow.

Session 02 - Learning the Java Language

You might also like