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
p
o
n
e
n
t
 
a
r
c
h
i
t
e
c
t
u
r
e
s
.
O
b
j
e
c
t
 
s
e
r
i
a
l
i
z
a
t
i
o
n
:
 
A
l
l
o
w
s
 
l
i
g
h
t
w
e
i
g
h
t
 
p
e
r
s
i
s
t
e
n
c
e
 
a
n
d
c
o
m
m
u
n
i
c
a
t
i
o
n
 
v
i
a
 
R
e
m
o
t
e
 
M
e
t
h
o
d
 
I
n
v
o
c
a
t
i
o
n
 
(
R
M
I
)
.
J
a
v
a
 
D
a
t
a
b
a
s
e
 
C
o
n
n
e
c
t
i
v
i
t
y
 
(
J
D
B
C
T
M
)
:
 
P
r
o
v
i
d
e
s
u
n
i
f
o
r
m
 
a
c
c
e
s
s
 
t
o
 
a
 
w
i
d
e
 
r
a
n
g
e
 
o
f
 
r
e
l
a
t
i
o
n
a
l
 
d
a
t
a
b
a
s
e
s
.
Java – Overview and Basics
SDK & JRE
Standard Development Kit
Java Runtime Enviroment
Java – Overview and Basics
Linux installation instructions
1)  
Copy j2sdk-1_6_0_<version number>-linux-i586.bin to the
directory into which you want to install the Java 2 SDK.
(
e
x
a
m
p
l
e
:
)
 
/
u
s
r
/
l
o
c
a
l
/
2
)
 
 
R
u
n
 
j
2
s
d
k
-
1
_
6
_
0
_
<
v
e
r
s
i
o
n
 
n
u
m
b
e
r
>
-
l
i
n
u
x
-
i
5
8
6
.
b
i
n
 
chmod a+x j2sdk-1_6_0_<version number>-linux-i586.bin
 
./j2sdk-1_6_0_<version number>-linux-i586.bin
3
)
 
S
e
t
 
e
n
v
i
r
o
m
e
n
t
a
l
 
v
a
r
i
a
b
l
e
s
 
t
o
 
p
o
i
n
t
 
j
d
k
 
i
n
s
t
a
l
l
a
t
i
o
n
:
 
export PATH=$PATH:/pathtojdk/bin
 
export CLASSPATH=$CLASSPATH:/pathtojdk/lib:.
 
(example:)
 
export PATH=$PATH:/usr/local/j2sdk-1_4_0_01/bin
Java – Overview and Basics
Linux installation instructions(2)
Q
u
e
s
t
i
o
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
 
i
n
t
o
 
i
n
s
t
r
u
c
t
i
o
n
s
 
t
h
a
t
 
y
o
u
r
 
c
o
m
p
u
t
e
r
 
c
a
n
 
u
n
d
e
r
s
t
a
n
d
.
Java – Overview and Basics
Creating first application
1. Write following code:
class HelloWorldApp {
  public static void main(String[] args) {
  System.out.println("Hello World!"); // Display "Hello World!"
 } }
2. Compile it:
 
javac HelloWorldApp.java
3. Run the program:
 
java HelloWorldApp 
Java – Overview and Basics
Creating first applet
1.
Write following code:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25); // Display "Hello world!"
} }
Java – Overview and Basics
Creating first applet (2)
2. Write HTML file (HelloWorld.html):
<HTML>
<HEAD> <TITLE>The Hello World Applet</TITLE> </HEAD>
<BODY>
<APPLET CODE="HelloWorld.class" WIDTH=150
HEIGHT=25> </APPLET> </BODY> </HTML>
3. Compile the source file:
 
javac HelloWorld.java
4. Run the program:
 
appletviewer HelloWorld.html
Java – Overview and Basics
Comments in Java Code
T
h
e
 
J
a
v
a
 
l
a
n
g
u
a
g
e
 
s
u
p
p
o
r
t
s
 
t
h
r
e
e
 
k
i
n
d
s
 
o
f
 
c
o
m
m
e
n
t
s
:
/* text */
 
The compiler ignores everything from /* to */.
/** documentation */
 
This indicates a documentation comment The compiler ignores this
kind of comment, just like it ignores comments that use /* and */. The
JDK javadoc tool uses doc comments when preparing automatically
generated documentation.
// text
 
The compiler ignores everything from // to the end of the line.
Java – Overview and Basics
Defining a class
Class (template)
Instanciation – creation of  an Object
Variables
Methods
Java – Overview and Basics
The main method
p
u
b
l
i
c
 
s
t
a
t
i
c
 
v
o
i
d
 
m
a
i
n
(
S
t
r
i
n
g
[
]
 
a
r
g
s
)
1.
To launch an application is necessary to implement this
method.
 
If no, the similar error message is displayed by compiler:
 
In class NoMain: void main(String argv[]) is not defined
2. The main method accepts a single argument: an array of
elements of type String.
Java – Overview and Basics
Using an instance method or variable
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
H
e
l
l
o
W
o
r
l
d
)
;
S
y
s
t
e
m
 
 
c
l
a
s
s
S
y
s
t
e
m
.
o
u
t
 
 
f
u
l
l
 
n
a
m
e
 
o
f
 
v
a
r
i
a
b
l
e
 
o
u
t
.
 
W
h
e
n
 
t
h
e
 
S
y
s
t
e
m
c
l
a
s
s
 
i
s
 
l
o
a
d
e
d
 
i
n
t
o
 
t
h
e
 
a
p
p
l
i
c
a
t
i
o
n
,
 
i
t
 
i
n
s
t
a
n
t
i
a
t
e
s
P
r
i
n
t
S
t
r
e
a
m
 
a
n
d
 
a
s
s
i
g
n
s
 
t
h
e
 
n
e
w
 
P
r
i
n
t
S
t
r
e
a
m
 
o
b
j
e
c
t
 
t
o
t
h
e
 
o
u
t
 
c
l
a
s
s
 
v
a
r
i
a
b
l
e
P
r
i
n
t
S
t
r
e
a
m
 
 
t
y
p
e
 
o
f
 
o
b
j
e
c
t
 
o
u
t
.
 
I
t
 
h
a
s
 
m
e
t
h
o
d
:
p
r
i
n
t
l
n
(
S
t
r
i
n
g
)
;
Java – Overview and Basics
Importing classes and packages
1
)
 
i
m
p
o
r
t
 
j
a
v
a
.
a
p
p
l
e
t
.
A
p
p
l
e
t
;
i
m
p
o
r
t
 
j
a
v
a
.
a
w
t
.
G
r
a
p
h
i
c
s
;
 
public class HelloWorld extends Applet {
 
 public void paint(Graphics g) { g.drawString("Hello
world!", 50, 25); } }
2)
p
u
b
l
i
c
 
c
l
a
s
s
 
H
e
l
l
o
W
o
r
l
d
 
e
x
t
e
n
d
s
 
j
a
v
a
.
a
p
p
l
e
t
.
A
p
p
l
e
t
 
{
p
u
b
l
i
c
 
v
o
i
d
 
p
a
i
n
t
(
j
a
v
a
.
a
w
t
.
G
r
a
p
h
i
c
s
 
g
)
 
{
g
.
d
r
a
w
S
t
r
i
n
g
(
"
H
e
l
l
o
 
w
o
r
l
d
!
"
,
 
5
0
,
 
2
5
)
;
 
}
 
}
 
Java – Overview and Basics
Importing classes and packages (2)
P
a
c
k
a
g
e
s
 
a
r
e
 
u
s
e
d
 
t
o
 
g
r
o
u
p
 
c
l
a
s
s
e
s
,
 
s
i
m
i
l
a
r
 
t
o
 
t
h
e
 
w
a
y
l
i
b
r
a
r
i
e
s
 
a
r
e
 
u
s
e
d
 
t
o
 
g
r
o
u
p
 
C
 
f
u
n
c
t
i
o
n
s
.
Every class is in package
I
f
 
t
h
e
 
s
o
u
r
c
e
 
c
o
d
e
 
f
o
r
 
a
 
c
l
a
s
s
 
d
o
e
s
n
'
t
 
h
a
v
e
 
a
 
p
a
c
k
a
g
e
s
t
a
t
e
m
e
n
t
 
a
t
 
t
h
e
 
t
o
p
,
 
d
e
c
l
a
r
i
n
g
 
t
h
e
 
p
a
c
k
a
g
e
 
t
h
e
 
c
l
a
s
s
 
i
s
i
n
,
 
t
h
e
n
 
t
h
e
 
c
l
a
s
s
 
i
s
 
i
n
 
t
h
e
 
d
e
f
a
u
l
t
 
p
a
c
k
a
g
e
.
Within a package, all classes can refer to each other
without prefixes. For example, the java.awt Component
class refers to the java.awt Graphics class without any
prefixes, without importing the Graphics class.
Java – Overview and Basics
Common Compiler problem
C
a
n
'
t
 
L
o
c
a
t
e
 
t
h
e
 
C
o
m
p
i
l
e
r
 
javac: Command not found
S
o
l
u
t
i
o
n
:
 
Modify your PATH environment variable so that it includes
the directory where the Java compiler lives.
Java – Overview and Basics
Common Interpreter problem
C
a
n
'
t
 
F
i
n
d
 
C
l
a
s
s
 
Can't find class HelloWorldApp.class
S
o
l
u
t
i
o
n
:
T
h
e
 
a
r
g
u
m
e
n
t
 
t
o
 
t
h
e
 
J
a
v
a
 
i
n
t
e
r
p
r
e
t
e
r
 
i
s
 
t
h
e
 
n
a
m
e
 
o
f
 
t
h
e
 
c
l
a
s
s
t
h
a
t
 
y
o
u
 
w
a
n
t
 
t
o
 
u
s
e
,
 
n
o
t
 
t
h
e
 
f
i
l
e
n
a
m
e
 
(
H
e
l
l
o
W
o
r
l
d
A
p
p
i
n
s
t
e
a
d
 
o
f
 
H
e
l
l
o
W
o
r
l
d
A
p
p
.
c
l
a
s
s
)
Java – Overview and Basics
Java language
Java – Overview and Basics
Object Oriented Programming (OOP) concepts
A
n
 
o
b
j
e
c
t
 
i
s
 
a
 
s
o
f
t
w
a
r
e
 
b
u
n
d
l
e
 
o
f
 
v
a
r
i
a
b
l
e
s
 
a
n
d
 
r
e
l
a
t
e
d
m
e
t
h
o
d
s
.
 
Visual
representation of a
software object:
Bicycle modeled
as a software
object:
Java – Overview and Basics
OOP concepts (2)
E
n
c
a
p
s
u
l
a
t
i
o
n
 
b
e
n
e
f
i
t
s
:
M
o
d
u
l
a
r
i
t
y
:
 
T
h
e
 
s
o
u
r
c
e
 
c
o
d
e
 
f
o
r
 
a
n
 
o
b
j
e
c
t
 
c
a
n
 
b
e
 
w
r
i
t
t
e
n
 
a
n
d
m
a
i
n
t
a
i
n
e
d
 
i
n
d
e
p
e
n
d
e
n
t
l
y
 
o
f
 
t
h
e
 
s
o
u
r
c
e
 
c
o
d
e
 
f
o
r
 
o
t
h
e
r
o
b
j
e
c
t
s
.
 
A
l
s
o
,
 
a
n
 
o
b
j
e
c
t
 
c
a
n
 
b
e
 
e
a
s
i
l
y
 
p
a
s
s
e
d
 
a
r
o
u
n
d
 
i
n
 
t
h
e
s
y
s
t
e
m
.
I
n
f
o
r
m
a
t
i
o
n
 
h
i
d
i
n
g
:
 
A
n
 
o
b
j
e
c
t
 
h
a
s
 
a
 
p
u
b
l
i
c
 
i
n
t
e
r
f
a
c
e
 
t
h
a
t
 
o
t
h
e
r
o
b
j
e
c
t
s
 
c
a
n
 
u
s
e
 
t
o
 
c
o
m
m
u
n
i
c
a
t
e
 
w
i
t
h
 
i
t
.
 
T
h
e
 
o
b
j
e
c
t
 
c
a
n
m
a
i
n
t
a
i
n
 
p
r
i
v
a
t
e
 
i
n
f
o
r
m
a
t
i
o
n
 
a
n
d
 
m
e
t
h
o
d
s
 
t
h
a
t
 
c
a
n
 
b
e
c
h
a
n
g
e
d
 
a
t
 
a
n
y
 
t
i
m
e
 
w
i
t
h
o
u
t
 
a
f
f
e
c
t
i
n
g
 
t
h
e
 
o
t
h
e
r
 
o
b
j
e
c
t
s
 
t
h
a
t
d
e
p
e
n
d
 
o
n
 
i
t
.
Java – Overview and Basics
OOP concepts (3)
M
e
s
s
a
g
e
S
o
f
t
w
a
r
e
 
o
b
j
e
c
t
s
 
i
n
t
e
r
a
c
t
 
a
n
d
 
c
o
m
m
u
n
i
c
a
t
e
 
w
i
t
h
 
e
a
c
h
 
o
t
h
e
r
b
y
 
s
e
n
d
i
n
g
 
m
e
s
s
a
g
e
s
 
t
o
 
e
a
c
h
 
o
t
h
e
r
.
 
W
h
e
n
 
o
b
j
e
c
t
 
A
 
w
a
n
t
s
o
b
j
e
c
t
 
B
 
t
o
 
p
e
r
f
o
r
m
 
o
n
e
 
o
f
 
B
'
s
 
m
e
t
h
o
d
s
,
 
o
b
j
e
c
t
 
A
 
s
e
n
d
s
 
a
m
e
s
s
a
g
e
 
t
o
 
o
b
j
e
c
t
 
B
Java – Overview and Basics
OOP concepts (4)
T
h
e
 
t
h
r
e
e
 
c
o
m
p
o
n
e
n
t
s
 
o
f
 
a
 
m
e
s
s
a
g
e
:
1) The object to which the message is addressed
(YourBicycle)
2) The name of the method to perform (changeGears)
3) Any parameters needed by the method (lowerGear)
Java – Overview and Basics
OOP concepts (5)
A
 
c
l
a
s
s
 
i
s
 
a
 
p
r
o
t
o
t
y
p
e
 
t
h
a
t
 
d
e
f
i
n
e
s
 
t
h
e
 
v
a
r
i
a
b
l
e
s
 
a
n
d
 
t
h
e
m
e
t
h
o
d
s
 
c
o
m
m
o
n
 
t
o
 
a
l
l
 
o
b
j
e
c
t
s
 
o
f
 
a
 
c
e
r
t
a
i
n
 
k
i
n
d
.
Visual
representation
of class:
Visual
representation
of bike class:
Java – Overview and Basics
OOP concepts (6)
I
n
h
e
r
i
t
a
n
c
e
S
u
p
e
r
c
l
a
s
s
S
u
b
c
l
a
s
s
Java – Overview and Basics
OOP concepts (7)
I
n
h
e
r
i
t
a
n
c
e
 
b
e
n
e
f
i
t
s
:
Subclasses provide specialized behaviors from the basis of
common elements provided by the superclass. Through the
use of inheritance, programmers can reuse the code in the
superclass many times.
Programmers can implement superclasses called 
abstract
classes
 that define "generic" behaviors. The abstract
superclass defines and may partially implement the behavior,
but much of the class is undefined and unimplemented.
Other programmers fill in the details with specialized
subclasses.
Java – Overview and Basics
OOP concepts (8)
A
n
 
i
n
t
e
r
f
a
c
e
 
i
s
 
a
 
d
e
v
i
c
e
 
t
h
a
t
 
u
n
r
e
l
a
t
e
d
 
o
b
j
e
c
t
s
 
u
s
e
 
t
o
 
i
n
t
e
r
a
c
t
w
i
t
h
 
e
a
c
h
 
o
t
h
e
r
.
 
I
t
 
i
s
 
m
o
s
t
 
a
n
a
l
o
g
o
u
s
 
t
o
 
a
 
p
r
o
t
o
c
o
l
 
(
a
n
 
a
g
r
e
e
d
o
n
 
b
e
h
a
v
i
o
r
)
.
E
x
a
m
p
l
e
:
 
I
n
v
e
n
t
o
r
y
 
I
n
t
e
r
f
a
c
e
  
To work in the inventory program, the bicycle class must
 
agree to this protocol by implementing the interface.
W
h
e
n
 
a
 
c
l
a
s
s
 
i
m
p
l
e
m
e
n
t
s
 
a
n
 
i
n
t
e
r
f
a
c
e
,
 
t
h
e
 
c
l
a
s
s
 
a
g
r
e
e
s
 
t
o
i
m
p
l
e
m
e
n
t
 
a
l
l
 
t
h
e
 
m
e
t
h
o
d
s
 
d
e
f
i
n
e
d
 
i
n
 
t
h
e
 
i
n
t
e
r
f
a
c
e
.
Java – Overview and Basics
OOP concepts (9)
I
n
t
e
r
f
a
c
e
s
 
b
e
n
e
f
i
t
s
:
Capturing similarities among unrelated classes without
forcing a class relationship.
Declaring methods that one or more classes are expected to
implement.
Revealing an object's programming interface without
revealing its class.
Java – Overview and Basics
Variables
An object stores its state in variables.
A
 
v
a
r
i
a
b
l
e
 
i
s
 
a
n
 
i
t
e
m
 
o
f
 
d
a
t
a
 
n
a
m
e
d
 
b
y
 
a
n
 
i
d
e
n
t
i
f
i
e
r
.
The variable's type determines what values it can hold and
what operations can be performed on it.
T
o
 
g
i
v
e
 
a
 
v
a
r
i
a
b
l
e
 
a
 
t
y
p
e
 
a
n
d
 
a
 
n
a
m
e
,
 
y
o
u
 
w
r
i
t
e
 
a
 
v
a
r
i
a
b
l
e
d
e
c
l
a
r
a
t
i
o
n
,
 
w
h
i
c
h
 
g
e
n
e
r
a
l
l
y
 
l
o
o
k
s
 
l
i
k
e
 
t
h
i
s
:
type name
A
 
v
a
r
i
a
b
l
e
 
h
a
s
 
s
c
o
p
e
.
Java – Overview and Basics
Variables (2)
E
v
e
r
y
 
v
a
r
i
a
b
l
e
 
m
u
s
t
 
h
a
v
e
 
a
 
d
a
t
a
 
t
y
p
e
J
a
v
a
 
h
a
s
 
t
w
o
 
c
a
t
e
g
o
r
i
e
s
 
o
f
 
d
a
t
a
 
t
y
p
e
s
:
 
p
r
i
m
i
t
i
v
e
 
a
n
d
 
r
e
f
e
r
e
n
c
e
A variable of primitive type contains a single value of the appropriate size
and format for its type: a number, a character, or a boolean value
Arrays, classes, and interfaces are reference types. The value of a
reference type variable, in contrast to that of a primitive type, is a
reference to (an address of) the value or set of values represented by the
variable.
A reference is called a pointer, or a memory address. The Java does not
support the explicit use of addresses like other languages do. You use
the variable's name instead.
Java – Overview and Basics
Variables (3)
P
r
i
m
i
t
i
v
e
 
d
a
t
a
 
t
y
p
e
s
:
K
e
y
w
o
r
d
 
S
i
z
e
/
F
o
r
m
a
t
byte 
  
8-bit
short 
  
16-bit
int 
   
32-bit
long 
  
64-bit
float 
  
32-bit
double 
 
64-bit
char 
  
16-bit
boolean 
 
true or false
 
T
h
e
 
f
o
r
m
a
t
 
a
n
d
 
s
i
z
e
 
o
f
p
r
i
m
i
t
i
v
e
 
d
a
t
a
 
t
y
p
e
s
 
i
s
i
n
d
e
p
e
n
d
e
n
t
 
f
r
o
m
 
t
h
e
p
l
a
t
f
o
r
m
 
o
n
 
w
h
i
c
h
 
a
p
r
o
g
r
a
m
 
i
s
 
r
u
n
n
i
n
g
 
!
Java – Overview and Basics
Variables (4)
V
a
r
i
a
b
l
e
 
n
a
m
e
:
1)  It must be a legal identifier. An identifier is an unlimited series
of Unicode characters that begins with a letter.
2 ) It must not be a keyword, a boolean literal (true or false), or
the reserved word null.
3) It must be unique within its scope.
 
Java – Overview and Basics
Variables (5)
B
y
 
C
o
n
v
e
n
t
i
o
n
 
:
Variable names begin with a lowercase letter.
Class names begin with an uppercase letter.
If a variable name consists of more than one word, the
words are joined together, and each word after the first
begins with an uppercase letter, like this: 
isVisible.
The underscore character (_) is acceptable anywhere in a
name, but by convention is used only to separate words
in constants (because constants are all caps by
convention and thus cannot be case-delimited).
 
Java – Overview and Basics
Variables (6)
A
 
v
a
r
i
a
b
l
e
'
s
 
s
c
o
p
e
 
i
s
 
t
h
e
 
r
e
g
i
o
n
 
o
f
 
a
 
p
r
o
g
r
a
m
 
w
i
t
h
i
n
 
w
h
i
c
h
 
t
h
e
 
v
a
r
i
a
b
l
e
c
a
n
 
b
e
 
r
e
f
e
r
r
e
d
 
t
o
 
b
y
 
i
t
s
 
s
i
m
p
l
e
 
n
a
m
e
.
The location of the variable declaration within your program establishes
its scope and places it into one of these four categories:
 
Java – Overview and Basics
Variables (7)
T
h
e
 
v
a
l
u
e
 
o
f
 
a
 
f
i
n
a
l
 
v
a
r
i
a
b
l
e
 
c
a
n
n
o
t
 
c
h
a
n
g
e
 
a
f
t
e
r
 
i
t
 
h
a
s
 
b
e
e
n
i
n
i
t
i
a
l
i
z
e
d
.
 
S
u
c
h
 
v
a
r
i
a
b
l
e
s
 
a
r
e
 
s
i
m
i
l
a
r
 
t
o
 
c
o
n
s
t
a
n
t
s
 
i
n
 
o
t
h
e
r
p
r
o
g
r
a
m
m
i
n
g
 
l
a
n
g
u
a
g
e
s
.
To declare a final variable, use the final keyword in the
variable declaration before the type:
final int aFinalVar = 0
;
It is possible declare the local variable and initialize it later
(but only once):
final int blankfinal;
. . .
blankfinal = 0;
 
Java – Overview and Basics
Variables (8)
Question:
W
h
i
c
h
 
o
f
 
t
h
e
 
f
o
l
l
o
w
i
n
g
 
a
r
e
 
v
a
l
i
d
 
v
a
r
i
a
b
l
e
 
n
a
m
e
s
?
int
anInt
i
i1
1
thing1
1thing
ONE-HUNDRED
ONE_HUNDRED
 
 
Question:
W
h
i
c
h
 
o
f
 
t
h
e
 
f
o
l
l
o
w
i
n
g
 
a
r
e
 
v
a
l
i
d
 
v
a
r
i
a
b
l
e
 
n
a
m
e
s
?
 
A
n
s
w
e
r
:
int
anInt
i
i1
1
thing1
1thing
ONE-HUNDRED
ONE_HUNDRED
Java – Overview and Basics
Operators
A
n
 
o
p
e
r
a
t
o
r
 
p
e
r
f
o
r
m
s
 
a
 
f
u
n
c
t
i
o
n
 
o
n
 
o
n
e
,
 
t
w
o
,
 
o
r
 
t
h
r
e
e
o
p
e
r
a
n
d
s
.
U
n
a
r
y
 
o
p
e
r
a
t
o
r
s
 
(
e
x
a
m
p
l
e
:
 
+
+
)
 
(
p
o
s
t
f
i
x
 
a
n
d
 
p
r
e
f
i
x
)
B
i
n
a
r
y
 
o
p
e
r
a
t
o
r
s
 
(
e
x
a
m
p
l
e
:
 
+
)
 
(
i
n
f
i
x
)
T
e
r
n
a
r
y
 
o
p
e
r
a
t
o
r
s
 
(
 
e
x
a
m
p
l
e
:
 
?
:
)
 
(
i
n
f
i
x
)
 
Java – Overview and Basics
Operators (2)
A
r
i
t
h
m
e
t
i
c
 
o
p
e
r
a
t
o
r
s
:
 
Java – Overview and Basics
Operators (3)
A
r
i
t
h
m
e
t
i
c
 
o
p
e
r
a
t
o
r
s
 
-
 
c
o
n
v
e
r
s
i
o
n
s
:
 
Java – Overview and Basics
Operators (4)
U
n
a
r
y
 
o
p
e
r
a
t
o
r
s
 
:
 
Java – Overview and Basics
Operators (5)
p
u
b
l
i
c
 
c
l
a
s
s
 
S
o
r
t
D
e
m
o
 
{
 
public static void main(String[] args) {
i
n
t
[
]
 
a
r
r
a
y
O
f
I
n
t
s
 
=
 
{
 
3
2
,
 
8
7
,
 
3
,
 
5
8
9
,
 
1
2
,
 
8
,
 
6
2
2
,
 
1
2
7
 
}
;
f
o
r
 
(
i
n
t
 
i
 
=
 
a
r
r
a
y
O
f
I
n
t
s
.
l
e
n
g
t
h
;
 
-
-
i
 
>
=
 
0
;
 
)
 
{
f
o
r
 
(
i
n
t
 
j
 
=
 
0
;
 
j
 
<
 
i
;
 
j
+
+
)
 
{
    
if (arrayOfInts[j] > arrayOfInts[j+1]) {
 
     
int temp = arrayOfInts[j];
     
arrayOfInts[j] = arrayOfInts[j+1];
     
arrayOfInts[j+1] = temp;
}
}
}
}
}
 
Java – Overview and Basics
Operators (6)
E
q
u
a
l
i
t
y
 
a
n
d
 
R
e
l
a
t
i
o
n
a
l
 
o
p
e
r
a
t
o
r
s
 
:
 
Java – Overview and Basics
Operators (7)
C
o
n
d
i
t
i
o
n
a
l
 
o
p
e
r
a
t
o
r
s
 
:
 
Java – Overview and Basics
Operators (8)
B
i
t
 
S
h
i
f
t
 
o
p
e
r
a
t
o
r
s
 
:
 
Q
u
e
s
t
i
o
n
:
1
3
 
 
>
>
 
1
 
=
 
?
 
 
A
n
s
w
e
r
:
6
1
3
(
1
1
0
1
)
 
>
>
 
1
 
=
 
6
 
(
0
1
1
0
)
Java – Overview and Basics
Operators (9)
B
i
t
w
i
s
e
 
o
p
e
r
a
t
o
r
s
 
:
 
Java – Overview and Basics
Operators (10)
A
s
s
i
g
n
m
e
n
t
s
 
o
p
e
r
a
t
o
r
s
 
(
e
x
a
m
p
l
e
s
)
:
 
Java – Overview and Basics
Operators (11)
O
t
h
e
r
 
o
p
e
r
a
t
o
r
s
:
 
Java – Overview and Basics
Operators (12)
O
t
h
e
r
 
o
p
e
r
a
t
o
r
s
:
T
h
e
 
n
e
w
 
o
p
e
r
a
t
o
r
 
c
r
e
a
t
e
s
 
a
 
n
e
w
 
o
b
j
e
c
t
 
o
r
 
a
 
n
e
w
 
a
r
r
a
y
.
E
x
a
m
p
l
e
:
 
c
r
e
a
t
i
n
g
 
a
 
n
e
w
 
I
n
t
e
g
e
r
 
o
b
j
e
c
t
 
f
r
o
m
 
t
h
e
 
I
n
t
e
g
e
r
 
c
l
a
s
s
 
i
n
 
t
h
e
 
j
a
v
a
.
l
a
n
g
p
a
c
k
a
g
e
:
Integer anInteger = new Integer(10);
T
h
e
 
i
n
s
t
a
n
c
e
o
f
 
o
p
e
r
a
t
o
r
 
t
e
s
t
s
 
w
h
e
t
h
e
r
 
i
t
s
 
f
i
r
s
t
 
o
p
e
r
a
n
d
 
i
s
 
a
n
instance of its second.
op1 instanceof op2
o
p
1
 
m
u
s
t
 
b
e
 
t
h
e
 
n
a
m
e
 
o
f
 
a
n
 
o
b
j
e
c
t
 
a
n
d
 
o
p
2
 
m
u
s
t
 
b
e
 
t
h
e
n
a
m
e
 
o
f
 
a
 
c
l
a
s
s
.
 
A
n
 
o
b
j
e
c
t
 
i
s
 
c
o
n
s
i
d
e
r
e
d
 
t
o
 
b
e
 
a
n
 
i
n
s
t
a
n
c
e
 
o
f
 
a
 class if that object directly or indirectly descends from that
 class.
 
Operators (13) – instanceOf
Java – Overview and Basics
56
56
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
        Parent obj1 = new Parent();
        Parent obj2 = new Child();
        System.out.println("obj1 instanceof Parent:” + (obj1 instanceof Parent));
O
u
t
p
u
t
 
?
?
?
obj1 instanceof Parent: 
  
true or false ???
obj1 instanceof Child: 
  
true or false ???
obj1 instanceof MyInterface:
 
true or false ???
obj2 instanceof Parent: 
  
true or false ???
obj2 instanceof Child: 
  
true or false ???
obj2 instanceof MyInterface: 
 
true or false ???
 
t
r
u
e
f
a
l
s
e
f
a
l
s
e
t
r
u
e
t
r
u
e
t
r
u
e
Operators (14) – questions
Java – Overview and Basics
57
57
1)
int i = 
2
0;
int n = i++%
4
;
What are the values of i and n after the code is executed?
2)
int i = 
2
0;
int n = 
++
i%
5
;
What are the values of i and n after the code is executed?
Java – Overview and Basics
Operators (15)
O
p
e
r
a
t
o
r
s
 
p
r
e
c
e
d
e
n
c
e
:
[] . (
params
) 
expr
++ 
expr
--
++
expr
 --
expr
 +
expr
 -
expr
 ~ !
new (
type
)
expr
* / %
+ -
<< >> >>>
< > <= >= 
== !=
&
 
^
|
&&
||
? :
= += -= *= /= %= &= ^= |= <<=
>>= >>>=
Java – Overview and Basics
Control Flow Statements
 
 
Java – Overview and Basics
While, do-while and for
w
h
i
l
e
 
(
e
x
p
r
e
s
s
i
o
n
)
 
{
s
t
a
t
e
m
e
n
t
(
s
)
}
d
o
 
{
s
t
a
t
e
m
e
n
t
(
s
)
}
 
w
h
i
l
e
 
(
e
x
p
r
e
s
s
i
o
n
)
;
 
f
o
r
 
(
i
n
i
t
i
a
l
i
z
a
t
i
o
n
;
 
t
e
r
m
i
n
a
t
i
o
n
;
i
n
c
r
e
m
e
n
t
)
 
{
s
t
a
t
e
m
e
n
t
}
f
o
r
 
(
 
;
 
;
 
)
 
{
/
/
 
i
n
f
i
n
i
t
e
 
l
o
o
p
.
.
.
 
}
Java – Overview and Basics
If, if-else and switch
i
f
 
(
e
x
p
r
e
s
s
i
o
n
)
 
{
s
t
a
t
e
m
e
n
t
(
s
)
}
i
f
 
(
e
x
p
r
e
s
s
i
o
n
)
 
{
/
/
 
c
o
d
e
 
t
o
 
p
e
r
f
o
r
m
 
t
r
u
e
}
 
e
l
s
e
 
{
/
/
 
c
o
d
e
 
t
o
 
p
e
r
f
o
r
m
 
f
a
l
s
e
}
 
s
w
i
t
c
h
 
(
V
a
r
i
a
b
l
e
)
 
{
c
a
s
e
 
1
:
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
1
"
)
;
 
b
r
e
a
k
;
d
e
f
a
u
l
t
:
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
T
h
e
n
u
m
b
e
r
 
i
s
 
w
r
o
n
g
 
!
"
)
;
b
r
e
a
k
;
}
switch
 works with:
 
byte
short
char
,
 i
nt
 
enumerated types
 (Enum),
 
String
  
Character
Byte
Short
,  
In
teger
 
 classes
Java – Overview and Basics
Try, catch and finally
t
r
y
 
{
 
/
/
 
o
r
 
t
r
y
 
(
)
 
c
a
l
l
e
d
 
t
r
y
-
w
i
t
h
-
r
e
s
o
u
r
c
e
s
s
t
a
t
e
m
e
n
t
(
s
)
}
c
a
t
c
h
 
(
e
x
c
e
p
t
i
o
n
t
y
p
e
 
n
a
m
e
)
 
{
s
t
a
t
e
m
e
n
t
(
s
)
}
f
i
n
a
l
l
y
 
{
s
t
a
t
e
m
e
n
t
(
s
)
}
 
Java – Overview and Basics
Break, continue and return
B
r
e
a
k
 
a
n
d
 
c
o
n
t
i
n
u
e
:
1) break;  || continue;
2) break label; || continue label;
E
x
a
m
p
l
e
 
(
b
y
 
a
n
a
l
o
g
y
 
i
s
 
c
o
n
t
i
n
u
e
)
:
...
search:
for ( ; i < arrayOfInts.length; i++) {
 
for (j = 0; j < arrayOfInts[i].length; j++) {
  
if (arrayOfInts[i][j] == searchfor) {
   
foundIt = true;
   
break search;
  
} } }
 
R
e
t
u
r
n
:
1) return value;
2) return;
Java – Overview and Basics
Control Flow Statements(2)
Q
u
e
s
t
i
o
n
:
W
h
a
t
'
s
 
w
r
o
n
g
 
w
i
t
h
 
t
h
e
 
f
o
l
l
o
w
i
n
g
 
c
o
d
e
 
:
if (i = 1) {
/* do something */
 }
 
 
A
n
s
w
e
r
:
C
o
n
d
i
t
i
o
n
 
i
s
 
A
L
W
A
Y
S
 
t
r
u
e
.
 
T
y
p
i
c
a
l
 
m
i
s
t
a
k
e
 
o
f
p
r
o
g
r
a
m
m
e
r
s
:
=
 
i
s
 
a
n
 
a
s
s
i
g
n
m
e
n
t
=
=
 
i
s
 
a
 
c
o
m
p
a
r
i
s
o
n
Ussually compiler warns about it.
Java – Overview and Basics
Control Flow Statements(3)
Q
u
e
s
t
i
o
n
:
W
h
a
t
 
i
s
 
a
n
 
o
u
t
p
u
t
 
i
f
 
a
N
u
m
b
e
r
 
=
 
2
?
if (aNumber >= 0)
 
if (aNumber == 0) System.out.println("first string");
 else System.out.println("second string");
System.out.println("third string");
 
 
A
n
s
w
e
r
:
second string
third string
Slide Note
Embed
Share

Java is a versatile programming language known for its simplicity, security, portability, and high performance. Its history dates back to 1990 when the concept was first suggested, leading to the creation of Java in 1995 by James Gosling. Over the years, Java has evolved with various versions introducing new features and improvements. The language is both compiled and interpreted, offering a flexible development environment. Java's platform consists of the Java Virtual Machine (Java VM) and the Java Application Programming Interface (Java API). Key features include objects, strings, threads, networking capabilities, internationalization support, and more, making it a popular choice for software development.

  • Java programming
  • Overview
  • Basics
  • Object-oriented
  • Portable

Uploaded on Sep 24, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Java overview and basics

  2. Java Overview and Basics Literature English-language sites: http://java.sun.com //still works http://www.javaworld.com http://www.javalobby.com Books: Thinking in Java Bruce Eckel English edition: http://www.bruceeckel.com (older free, newest payable) Polish Edition: http://www.helion.pl Thousands of others Core Java, Volume I - Fundamentals (9th edition) by Cay Horstmann and Gary Cornell "Head first Java" by Kathy Sierra and Bert Bates "Core Java, Volume II " Advanced Features (9th edition) by Cay Horstmann and Gary Cornell Polish-language sites: http://www.java.pl http://www.jdn.pl

  3. Java Overview and Basics What is Java? Programming language Platform Java language: Simple Architecture neutral Object oriented Portable Secure Distributed High performance Interpreted Multithreaded

  4. Java Overview and Basics Brief history 1990 suggestion in report Further concerning creation of new object oriented environment 1991 OAK ( Object Application Kernel ) language (James Gosling) 1995 new language name: Java 1996 - Netscape compatible with Java 1.0. Sun propagates Java 1.0 environment 2001 Java 1.4.0 over 2100 classes library 2004 Java 1.5.0 2007 Java 1.6.0 2019 Java 1.11.0

  5. Java Overview and Basics Java compiled and interpreted One compilation Many interpretations

  6. Java Overview and Basics Java compiled and interpreted

  7. Java Overview and Basics Java platform The Java platform has two components: The Java Virtual Machine (Java VM) The Java Application Programming Interface (Java API)

  8. Java Overview and Basics Java features The essentials: Objects, strings, threads, numbers, input and output, data structures, system properties, date and time, and so on. Applets: The set of conventions used by applets. Networking: URLs, TCP (Transmission Control Protocol), UDP (User Datagram Protocol) sockets, and IP (Internet Protocol) addresses. Internationalization: Help for writing programs that can be localized for users worldwide. Programs can automatically adapt to specific locales and be displayed in the appropriate language.

  9. Java Overview and Basics Java features (2) Security: Both low level and high level, including electronic signatures, public and private key management, access control, and certificates. Software components: Known as JavaBeansTM, can plug into existing component architectures. Object serialization: Allows lightweight persistence and communication via Remote Method Invocation (RMI). Java Database Connectivity (JDBCTM): Provides uniform access to a wide range of relational databases.

  10. Java Overview and Basics SDK & JRE Standard Development Kit Java Runtime Enviroment

  11. Java Overview and Basics Linux installation instructions 1) Copy j2sdk-1_6_0_<version number>-linux-i586.bin to the directory into which you want to install the Java 2 SDK. (example:) /usr/local/ 2) Run j2sdk-1_6_0_<version number>-linux-i586.bin chmod a+x j2sdk-1_6_0_<version number>-linux-i586.bin ./j2sdk-1_6_0_<version number>-linux-i586.bin 3) Set enviromental variables to point jdk installation: export PATH=$PATH:/pathtojdk/bin export CLASSPATH=$CLASSPATH:/pathtojdk/lib:. (example:) export PATH=$PATH:/usr/local/j2sdk-1_4_0_01/bin

  12. Java Overview and Basics Linux installation instructions(2) Question: What is the first thing you should check if the interpreter returns the error: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.java. Answer: Check your CLASSPATH. Probably current working directory is not included.

  13. Java Overview and Basics Important tools All tools are in pathtojdk/bin/ directory: javac - compiler, java - interpreter, javadoc generator of API documentation, appletviewer applet browser, jar tool for jar files jdb - debuggger,

  14. Java Overview and Basics Creating first application Create a Java source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. Compile the source file into a bytecode file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a bytecode file. Run the program contained in the bytecode file. The Java VM is implemented by a Java interpreter, java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

  15. Java Overview and Basics Creating first application 1. Write following code: class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display "Hello World!" } } 2. Compile it: javac HelloWorldApp.java 3. Run the program: java HelloWorldApp

  16. Java Overview and Basics Creating first applet 1. Write following code: import java.applet.*; import java.awt.*; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); // Display "Hello world!" } }

  17. Java Overview and Basics Creating first applet (2) 2. Write HTML file (HelloWorld.html): <HTML> <HEAD> <TITLE>The Hello World Applet</TITLE> </HEAD> <BODY> <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML> 3. Compile the source file: javac HelloWorld.java 4. Run the program: appletviewer HelloWorld.html

  18. Java Overview and Basics Comments in Java Code The Java language supports three kinds of comments: /* text */ The compiler ignores everything from /* to */. /** documentation */ This indicates a documentation comment The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation. // text The compiler ignores everything from // to the end of the line.

  19. Java Overview and Basics Defining a class Class (template) Instanciation creation of an Object Variables Methods

  20. Java Overview and Basics The main method public static void main(String[] args) 1. To launch an application is necessary to implement this method. If no, the similar error message is displayed by compiler: In class NoMain: void main(String argv[]) is not defined 2. The main method accepts a single argument: an array of elements of type String.

  21. Java Overview and Basics Using an instance method or variable System.out.println( HelloWorld ); System class System.out full name of variable out. When the System class is loaded into the application, it instantiates PrintStream and assigns the new PrintStream object to the out class variable PrintStream type of object out. It has method: println(String);

  22. Java Overview and Basics Importing classes and packages 1) import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } 2) public class HelloWorld extends java.applet.Applet { public void paint(java.awt.Graphics g) { g.drawString("Hello world!", 50, 25); } }

  23. Java Overview and Basics Importing classes and packages (2) Packages are used to group classes, similar to the way libraries are used to group C functions. Every class is in package If the source code for a class doesn't have a package statement at the top, declaring the package the class is in, then the class is in the default package. Within a package, all classes can refer to each other without prefixes. For example, the java.awt Component class refers to the java.awt Graphics class without any prefixes, without importing the Graphics class.

  24. Java Overview and Basics Common Compiler problem Can't Locate the Compiler javac: Command not found Solution: Modify your PATH environment variable so that it includes the directory where the Java compiler lives.

  25. Java Overview and Basics Common Interpreter problem Can't Find Class Can't find class HelloWorldApp.class Solution: The argument to the Java interpreter is the name of the class that you want to use, not the filename (HelloWorldApp instead of HelloWorldApp.class)

  26. Java Overview and Basics Java language

  27. Java Overview and Basics Object Oriented Programming (OOP) concepts An object is a software bundle of variables and related methods. Visual representation of a software object: Bicycle modeled as a software object:

  28. Java Overview and Basics OOP concepts (2) Encapsulation benefits: Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Also, an object can be easily passed around in the system. Information hiding: An object has a public interface that other objects can use to communicate with it. The object can maintain private information and methods that can be changed at any time without affecting the other objects that depend on it.

  29. Java Overview and Basics OOP concepts (3) Message Software objects interact and communicate with each other by sending messages to each other. When object A wants object B to perform one of B's methods, object A sends a message to object B

  30. Java Overview and Basics OOP concepts (4) The three components of a message: 1) The object to which the message is addressed (YourBicycle) 2) The name of the method to perform (changeGears) 3) Any parameters needed by the method (lowerGear)

  31. Java Overview and Basics OOP concepts (5) A class is a prototype that defines the variables and the methods common to all objects of a certain kind. Visual representation of class: Visual representation of bike class:

  32. Java Overview and Basics OOP concepts (6) Inheritance Superclass Subclass

  33. Java Overview and Basics OOP concepts (7) Inheritance benefits: Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times. Programmers can implement superclasses called abstract classes that define "generic" behaviors. The abstract superclass defines and may partially implement the behavior, but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

  34. Java Overview and Basics OOP concepts (8) An interface is a device that unrelated objects use to interact with each other. It is most analogous to a protocol (an agreed on behavior). Example: Inventory Interface To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface.

  35. Java Overview and Basics OOP concepts (9) Interfaces benefits: Capturing similarities among unrelated classes without forcing a class relationship. Declaring methods that one or more classes are expected to implement. Revealing an object's programming interface without revealing its class.

  36. Java Overview and Basics Variables An object stores its state in variables. A variable is an item of data named by an identifier. The variable's type determines what values it can hold and what operations can be performed on it. To give a variable a type and a name, you write a variable declaration, which generally looks like this: type name A variable has scope.

  37. Java Overview and Basics Variables (2) Every variable must have a data type Java has two categories of data types: primitive and reference A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean value Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable. A reference is called a pointer, or a memory address. The Java does not support the explicit use of addresses like other languages do. You use the variable's name instead.

  38. Java Overview and Basics Variables (3) Primitive data types: Keyword byte short int long float double char boolean Size/Format 8-bit 16-bit 32-bit 64-bit 32-bit 64-bit 16-bit true or false The format and size of primitive data types is independent from the platform on which a program is running !

  39. Java Overview and Basics Variables (4) Variable name: 1) It must be a legal identifier. An identifier is an unlimited series of Unicode characters that begins with a letter. 2 ) It must not be a keyword, a boolean literal (true or false), or the reserved word null. 3) It must be unique within its scope.

  40. Java Overview and Basics Variables (5) By Convention : Variable names begin with a lowercase letter. Class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. The underscore character (_) is acceptable anywhere in a name, but by convention is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited).

  41. Java Overview and Basics Variables (6) A variable's scope is the region of a program within which the variable can be referred to by its simple name. The location of the variable declaration within your program establishes its scope and places it into one of these four categories:

  42. Java Overview and Basics Variables (7) The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages. To declare a final variable, use the final keyword in the variable declaration before the type: final int aFinalVar = 0; It is possible declare the local variable and initialize it later (but only once): final int blankfinal; . . . blankfinal = 0;

  43. Java Overview and Basics Variables (8) Question: Question: Which of the following are valid variable names? Which of the following are valid variable names? Answer: int int anInt anInt i i i1 i1 1 1 thing1 thing1 1thing 1thing ONE-HUNDRED ONE-HUNDRED ONE_HUNDRED ONE_HUNDRED

  44. Java Overview and Basics Operators An operator performs a function on one, two, or three operands. Unary operators (example: ++) (postfix and prefix) Binary operators (example: +) (infix) Ternary operators ( example: ?:) (infix)

  45. Java Overview and Basics Operators (2) Arithmetic operators: Operator Use Description + op1 + op2 Adds op1 and op2 - op1 op2 Subtracts op2 from op1 * op1 * op2 Multiplies op1 by op2 / op1 / op2 Divides op1 by op2 % op1 % op2 Computes the remainder of dividing op1 by op2

  46. Java Overview and Basics Operators (3) Arithmetic operators - conversions: Data Type of Result Data Type of Operands long Neither operand is a float or a double (integer arithmetic); at least one operand is a long. int Neither operand is a float or a double (integer arithmetic); neither operand is a long. double At least one operand is a double. float At least one operand is a float; neither operand is a double.

  47. Java Overview and Basics Operators (4) Unary operators : Operator Use Description + + op Promotes op to int if it's a byte, short, or char Arithmetically negates op - op ++ op ++ Increments op by 1; evaluates to the value of op before it was incremented Increments ... (after) ++ ++ op -- op -- Decrements ... (before) -- -- op Decrements ... (after) ! ! op Inverts the value of the boolean

  48. Java Overview and Basics Operators (5) public class SortDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } } }

  49. Java Overview and Basics Operators (6) Equality and Relational operators : Operator Use Returns true if > op1 > op2 op1 is greater than op2 >= op1 >= op2 op1 is greater than or equal to op2 op1 is less than op2 < op1 < op2 <= op1 <= op2 op1 is less than or equal to op2 == op1 == op2 op1 and op2 are equal != op1 != op2 op1 and op2 are not equal

  50. Java Overview and Basics Operators (7) Conditional operators : Operator Use Explanation && op1 && op2 Conditional AND || op1 || op2 Conditional OR Operator ?: (if-then-else): int value1 = 1; int value2 = 2; int result; boolean someCondition = true; result = someCondition ? value1 : value2; System.out.println(result);

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#