0% found this document useful (0 votes)
15 views27 pages

Java Data Types and Graphics Overview

This document provides a summary of key concepts in Java including: 1. It introduces Java and its syntax which is similar to C and C++ but omits complex features. 2. It discusses Java data types including primitive types like integers, floating points, characters, booleans, and strings. 3. It covers variables, constants, expressions including arithmetic, relational, logical, and string operators. 4. It explains casting between data types and how Java performs automatic type conversions when possible to avoid data loss.

Uploaded by

amiraaboalnor
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)
15 views27 pages

Java Data Types and Graphics Overview

This document provides a summary of key concepts in Java including: 1. It introduces Java and its syntax which is similar to C and C++ but omits complex features. 2. It discusses Java data types including primitive types like integers, floating points, characters, booleans, and strings. 3. It covers variables, constants, expressions including arithmetic, relational, logical, and string operators. 4. It explains casting between data types and how Java performs automatic type conversions when possible to avoid data loss.

Uploaded by

amiraaboalnor
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

Computer Graphics

Java Applet
Lecture 1

Dr. Samah Adel


Table of Contents
1. Data Types and Variables
2. Integer and Real Number Types
3. Type Conversion
4. Boolean Type
5. Character and String Types

8
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

2
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

3
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.

4
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).

[Link] [Link] Interpreter (JVM)

Compiler

5
The Java platform


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

The price to pay is a slower execution.

[Link] [Link] Interpreter (JVM)

Interpreter (JVM)

Compiler
Interpreter (JVM)

6
A first example

/**
* Hello World Application
* Our first example
*/
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!"); // display output
}
}

$ javac [Link]

$ ls [Link]
[Link]

$ java HelloWorld Hello


World

7
Data Types
Fundamental types


Java provides ten fundamental types:

– integers: byte, short, int and long


– floating point: float and double. double bigger in size than float

– characters: char.
– boolean
– void without type

– String

10
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’; if one word ' '
String s = "abcd"; if more than one " "
x = 55;

11
Constants


Constants are declared with the word final in front.
The specification of the initial value is compulsory:
value will not change
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

14
Expressions


Java provides a rich set of expressions:

– Arithmetic
– Relational
– Logical
– Strings related

15
Arithmetic expressions


Java provides the usual set of arithmetic operators:
– addition (+)
– subtraction (-)
– division (/)
– multiplication (*)
– modulus (%)

16
Arithmetic operators

class Arithmetic {
public static void main(String[] args) { int x =
12;
int y = 2 * x;
[Link](y);
int z = (y - x) % 5;
[Link](z);
final float pi = 3.1415F;
float f = pi / 0.62F;
[Link](f);
}
}

$ java Arithmetic 24
2
5.0669355

17
Arithmetic operators


Shorthand operators are provided:

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

x += 5; // x = x + 5
[Link](x);

x *= 2; // x = x * 2
[Link](x);
}
}

$ java ShortHand
17
34
18
Arithmetic operators


Pre and post operators are also provided:

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

[Link](x++); // printed and then


incremented [Link](x);

[Link](++y); // incremented and then


printed [Link](y);
}
}

$ java Increment
12 13 13 13

19
Relational expressions


Java provides the following relational operators:
– equivalent (==) x = y assigning value of x to y
x ==y check equivalently
– not equivalent (!=)
– less than (<)
– greater that (>)
– less than or equal (<=)
– greater than or equal (>=)


Important: relational expressions always return a
boolean value.
20
Relational expressions

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

[Link](x < y);


[Link](x != y - 21);

boolean test = x >= 10; [Link](test);


}
}

$ java Boolean true


false
true

21
Logical Operators


Java provides the following operators:
– and (&&)
– or (||)
– not(!)


Important: The logical operators can only be
applied to boolean expressions and return a
boolean value.

25
Logical Operators

class Logical {
public static void main(String[] args)
{ int x = 12,y = 33;
double d = 2.45,e = 4.54;

[Link](x < y && d < e);


[Link](!(x < y));

boolean test = ’a’ > ’z’; z > a


[Link](test || d - 2.1 > 0);
} false true
}

$ java Logical true


false
true

26
String operators


Java provides many operators for Strings:
– Concatenation (+) put them together

– 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 !=.
27
String operators

class Strings {
public static void main(String[] args) {

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


[Link](s1);

int i = 35,j = 44;


[Link]("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
28
String operators

class Strings2 {
public static void main(String[] args)
{
s1 == s2 error as it string
String s1 = "Hello";
String s2 = "Hello";

[Link]([Link](s2));
[Link]([Link]("Hi"))
;
}
}

$ java Strings2
true
false

29
Casting


Java performs a automatic type conversion in the
values when there is no risk for data to be lost.

class TestWide {
public static void main(String[] args)
{
int a = ’x’; // ’x’ is a
character
long b = 34; // 34 is an int
float c = 1002; // 1002 is an int
double d = 3.45F; // 3.45F is a float
}
}

30
Casting


In order to specify conversions where data can be lost
it is necessary to use the cast operator.

class TestNarrow {
public static void main(String[] args)
{
long a = 34;
int b = (int)a; // a is a long
double d = 3.45;
float f = (float)d; // d is a double
}
}

31

Common questions

Powered by AI

Java treats strings as objects of the String class rather than primitive data types, enabling rich methods and operations beyond simple character storage or manipulation found in languages like C. In expressions, if an operation begins with a string using the '+' operator, Java converts subsequent expressions to strings, facilitating seamless concatenation. Comparing strings requires methods like .equals() rather than using relational operators like ==, ensuring accurate content comparison instead of reference checks. This distinction underscores the object-oriented nature of Java and its operations on high-level data types .

Java provides ten fundamental data types: byte, short, int, and long for integers; float and double for floating-point numbers; char for characters; boolean for truth values; void for return types; and String for character sequences. Unique to Java compared to languages like C++ are the string-related expressions and the heavy reliance on type-safe expressions. Java treats String as a fundamental type and has specific string operations, such as concatenation using the '+' operator, but does not support operator overloading as C++ does .

Java's automatic type conversion enhances reliability by preventing data loss when converting between compatible data types up the hierarchy, such as from an int to a long. However, this can impact performance when the system needs to frequently process conversions in performance-critical sections. Manual type conversions, using the cast operator, allow arbitrary conversions but require caution as they can introduce data loss if not properly controlled. Thus, while Java's type conversion mechanisms enhance programming convenience and safety, careful management is crucial to maintain performance integrity and data consistency during manual conversions .

Logical operators in Java, such as and (&&), or (||), and not (!), manipulate boolean expressions by determining the truth values of conditional statements. They are essential for constructing complex conditional logic, facilitating decisions in flow control structures like if-else and loops. These operators ensure concise evaluation of multiple conditions, enabling the creation of flexible and robust control mechanisms in Java programming. Their ability to short-circuit evaluations can also optimize performance by skipping unnecessary checks .

In Java, relational operators return a boolean value, providing a clear, intuitive mechanism for evaluating comparisons. Operators include ==, !=, <, >, <=, and >=. This is similar to other languages like C++, but Java enforces stricter type safety, preventing arbitrary type comparisons that might otherwise lead to undefined behavior. Java's approach reduces errors by ensuring comparisons are type-consistent, while languages like C++ can allow for more flexible, albeit riskier, operations with less type enforcement .

Java achieves platform independence by compiling Java programs into Java bytecode, which is a machine-independent representation. This bytecode is executed by the Java Virtual Machine (JVM), which interprets the bytecode for the specific architecture of the host machine. Thus, a compiled Java program can run on any device with a compatible JVM, regardless of the underlying hardware .

The Java Virtual Machine (JVM) plays a crucial role in the execution of Java bytecodes by acting as an interpreter that translates these bytecodes into native machine code for the host system. The JVM ensures that Java's 'write once, run anywhere' promise is fulfilled, as it abstracts the underlying hardware differences. This independence helps Java programs maintain platform neutrality and allows developers to focus on coding without worrying about system-specific implementations .

Java applets are designed to be executed under a Java-capable browser, while Java applications are standalone programs that can be executed independently of any web browser. This distinction arises because applets need to be embedded within an HTML page and have security restrictions, whereas applications do not have these constraints and are more suited for comprehensive standalone functionality .

Type safety in Java ensures that operations on data types are performed according to predefined rules, preventing unchecked memory access and erroneous behavior at runtime. Java enforces type safety by requiring type checks during compile time and runtime wherever necessary. This restricts arbitrary memory access, unlike languages like C or C++, where pointer arithmetic can lead to unpredictable behavior. Thus, type safety in Java protects programmers from memory corruption and aids in writing reliable applications .

Java's shorthand operators, such as +=, -=, *=, and /=, enhance code readability by making expressions more concise, reducing the verbosity of straightforward arithmetic operations. They also improve coding efficiency by simplifying code maintenance and updates. However, they can obscure the intent of code if overused, especially for those unfamiliar with shorthand notation. Thus, while they streamline code, their impact on readability depends on balanced use within the context .

You might also like