0% found this document useful (0 votes)
2 views

Lecture 1 Oop

The document provides an introduction to Java, highlighting its features as a powerful, general-purpose, object-oriented language with a syntax similar to C and C++. It discusses the Java platform, including the compilation to byte-code and execution by the Java Virtual Machine (JVM), as well as fundamental types, variables, literals, constants, and various expressions. Additionally, it covers arithmetic, relational, bit-level, logical, and string operators in Java with examples.

Uploaded by

lovesupra60
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1 Oop

The document provides an introduction to Java, highlighting its features as a powerful, general-purpose, object-oriented language with a syntax similar to C and C++. It discusses the Java platform, including the compilation to byte-code and execution by the Java Virtual Machine (JVM), as well as fundamental types, variables, literals, constants, and various expressions. Additionally, it covers arithmetic, relational, bit-level, logical, and string operators in Java with examples.

Uploaded by

lovesupra60
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to Java

Shahrukh Haider
2 Introduction

Java is a very powerful language that has generated
a lot of interest in the last years.

Java

The Star 7 device

The HotJava browser

Oak
3 Introduction

It is a general purpose concurrent object oriented language,
with a syntax similar to C and C++, but omitting features
that are complex and unsafe.
C++ Java
Backward compatile con C Backward compatibility with
previous Java versions
Execution efficiency Developer productivity
Trusts the programmer Protects the programmer
Arbitrary memory access possible Memory access through objects
Concise expression Explicit operation
Can arbitrarily override types Type safety
Procedural or object oriented Object oriented
Operator overloading Meaning of
operators
immutable
4 Introduction

The world wide web has popularized the use of Java,
because programs can be transparently downloaded with
web pages and executed in any computer with a Java
capable browser.

A Java application is a standalone Java program that can be
executed independently of any web browser.

A Java applet is a program designed to be executed
under a Java capable browser.
5 The Java platform

Java programs are compiled to Java byte-codes,
a kind of machine independent representation.
The program is then executed by an interpreter
called the Java Virtual Machine (JVM).

Test.java Test.class Interpreter (JVM)

Compiler
6 The Java platform

The compiled code is independent of
the architecture of the computer.

The price to pay is a slower execution.

Test.java Test.class Interpreter (JVM)

Interpreter (JVM)

Compiler
Interpreter (JVM)
7 A first example
/**
* Hello World Application
* Our first example
*/
public class HelloWorld {
public static void main(String[] args)
{ System.out.println("Hello World!"); // display
output
}
}

$ javac HelloWorld.java

$ ls
HelloWorld.class
HelloWorld.java

$ java HelloWorld
Hello World
8 Documentation

The javadoc utility can be used to generate
automatically documentation for the class.
/**
* My first <b>Test</b>
* @author Carlos Kavka
* @version 1.1
*/
public class HelloWorld {
/**
* @param args the command line arguments
* @since 1.0
*/
public static void main(String[] args) {
System.out.println("Hello World");
}
}
9 Documentation
10 Fundamental types

Java provides ten fundamental types:

- integers: byte, short, int and long


- floating point: float and double.
- characters: char.
- boolean
- void
- String
11 Variables

The variables are declared specifying its type and
name, and initialized in the point of declaration, or
later with the assignment expression:

int x;
double f = 0.33;
char c = ’a’;
String s = "abcd";

x = 55;
12 Literals

The integer values can be written in decimal,
hexadecimal, octal and long forms:
int x = 34; // decimal value
int y = 0x3ef; // hexadecimal
int z = 0772; // octal
long m = 240395922L; // long


The floating point values are of type double by
default:

double d = 6.28; // 6.28 is a double value


float f = 6.28F; // 6.28F is a float value
13 Literals

The character values are specified with the
standard C notation, with extensions for Unicode
values:
char c = ’a’; // character lowercase a
char d = ’\n’; // newline
char e = ’\u2122’ // unicode character (TM)


The boolean values are true and false:

boolean ready = true; // boolean value true


boolean late = false; // boolean value false
14 Constants

Constants are declared with the word final in front.
The specification of the initial value is compulsory:

final double pi = 3.1415; // constant PI


final int maxSize = 100; // integer constant
final char lastLetter = ’z’; // last lowercase letter
final String word = "Hello"; // a constant string
15 Expressions

Java provides a rich set of expressions:

- Arithmetic
- Bit level
- Relational
- Logical
- Strings
related
16 Arithmetic expressions

Java provides the usual set of arithmetic operators:
- addition (+)
- subtraction (-)
- division (/)
- multiplication (*)
- modulus (%)
17 Arithmetic operators
class Arithmetic {
public static void main(String[] args) {
int x = 12;
int y = 2 * x;
System.out.println(y);
int z = (y - x) % 5;
System.out.println(z);
final float pi = 3.1415F;
float f = pi / 0.62F;
System.out.println(f);
}
}

$ java Arithmetic
24
2
5.0669355
18 Arithmetic operators

Shorthand operators are provided:

class ShortHand {
public static void main(String[] args) {
int x = 12;

x += 5; // x =
x + 5 System.out.println(x);

x *= 2; // x =
x * 2 System.out.println(x);
}
}

$ java ShortHand
17
34
19 Arithmetic operators

Pre and post operators are also provided:

class Increment {
public static void main(String[] args) {
int x = 12,y = 12;

System.out.println(x++); // printed and then incremented


System.out.println(x);

System.out.println(++y); // incremented and then printed


System.out.println(y);
}
}

$ java Increment
12 13 13 13
20 Relational expressions

Java provides the following relational operators:
- equivalent (==)
- not equivalent (!=)
- less than (<)
- greater that (>)
-
less than or equal (<=)
- greater than or equal (>=)


Important: relational expressions always return a
boolean value.
21 Relational Expressions
class Boolean {
public static void main(String[] args) {
int x = 12,y = 33;

System.out.println(x < y);


System.out.println(x != y - 21);

boolean test = x >= 10;


System.out.println(test);
}
}

$ java Boolean
true
false
true
22 Bit level operators

Java provides the following operators:
- and (&)
- or (I)
-
not(C)
-
shift left
- (<<)

shift
- right with sign extension (>>)
• shift right with
Important: char,zero extension
short (>>>).
and byte arguments are
promoted to int before and the result is an int.
23 Bit level operators

class Bits {
public static void main(String[] args) {
int x = 0x16; // 00000000000000000000000000010110
int y = 0x33; // 00000000000000000000000000110011

System.out.println(x & y);// 00000000000000000000000000010010


System.out.println(x | y);// 00000000000000000000000000110111
System.out.println(˜x); // 11111111111111111111111111101001

x &= 0xf; // 00000000000000000000000000000110


System.out.println(x); // 00000000000000000000000000000110

short s = 7; // 0000000000000111
System.out.println(˜s); // 11111111111111111111111111111000
}
}
24 Bit level operators
class Bits2 {
public static void main(String[] args) {
int x = 0x16; //00000000000000000000000000010110
System.out.println(x << 3);//00000000000000000000000010110000

int y = 0xfe; //00000000000000000000000011111110


y >>= 4; //00000000000000000000000000001111
System.out.pr //00000000000000000000000000001111
intln(y);
x = 9; //00000000000000000000000000001001
System.out.println(x >> 3);//00000000000000000000000000000001
System.out.println(x >>>3);//00000000000000000000000000000001

x = -9; //11111111111111111111111111110111
System.out.println(x >> 3);//11111111111111111111111111111110
System.out.println(x >>>3);//00011111111111111111111111111110
}
}
25 Logical operators

Java provides the following operators:
- and (&&)
- or (II)
- not(!)


Important: The logical operators can only be
applied to boolean expressions and return a
boolean value.
26 Logical operators
class Logical {
public static void main(String[] args) {
int x = 12,y = 33;
double d = 2.45,e = 4.54;

System.out.println(x < y && d < e);


System.out.println(!(x < y));

boolean test = ’a’ > ’z’;


System.out.println(test || d - 2.1 > 0);
}
}

$ java Logical
true
false
true
27 String operators

Java provides many operators for Strings:
- Concatenation (+)
- many more...


Important: If the expression begins with a string
and uses the + operator, then the next argument is
converted to a string.


Important: Strings cannot be compared with ==
and !=.
28 String operators
class Strings {
public static void main(String[] args) {

String s1 = "Hello" + " World!";


System.out.println(s1);

int i = 35,j = 44;


System.out.println("The value of
i is " + i +
" and the
value of j is
" + j);
}
}
$ java Strings
Hello World!
The value of i
is 35 and the
value of j is
44
29 String operators
class Strings2 {
public static void main(String[] args) {

String s1 = "Hello";
String s2 = "Hello";

System.out.println(s1.equals(s2));
System.out.println(s1.equals("Hi"));
}
}

$ java Strings2
true
false

You might also like