Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

College of Information Engineering

Department of Computer Networks Engineering &


Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

Lecture 2
Java Basic
2.1 Data Representation in Java
Every variable must have a data type. The Java programming language has two
categories of data types: primitive and reference. A variable of primitive type
containing a single value of the appropriate size and format for its type: a number, a
character, or a boolean value.
The following table lists, by keyword, all of the primitive data types supported by the
Java platform, their sizes and formats, and a brief description of each.

2.1.1 Literals
Data is represented by literals in java. Java literals are based character and number
representations. The types of literals are integer, floating-point, Boolean, character,
and string.
Every variable consists of a literal and a data type. The difference between the two
is that literals are entered explicitly into the code. Data types are information about
the literals, such as how much room will be reserved in memory for that variable, as
well as the possible value ranges for the variable.
a) Integer Literals:
Integers are whole numbers, such as 1 and 5280. Integer literals can be
decimal (base 10) octal (base 8), or hexadecimal (base 16).
b) Floating-point Literals:
A floating-point literal represents a number that has a decimal point in it, such
as 3.7. That can be Single-precision or Double-precision numbers. The Single-
precision floating-point numbers consist of 32-bit space represents by (f). While,
Double-precision numbers consist of 64-bit space represents by (d). The Double-
precision floating-point numbers are the default. Therefore, 3.7 is a double-precision
floating-point number, and 3.7f is a single-precision floating-point number.

1
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

A Boolean literal is either of the words true or false. Unlike other programming
language, no numeric value such as 0 or 1 is assigned.

d) Character Literals:
The value of character literal is enclosed by single quotes, such as ’a’.

e) String Literals:
It’s a sequence of characters enclosed in double quotes, such as “This is a string
literal”. This could also be “Hello World” or even ” for a null character string. String
literals can be concatenated. For example, if one string contained “”This is the
beginning” and another string contained “of a beautiful relationship”, they could be
concatenated together. The representation would be “This is the beginning”+” of a
beautiful relationship”.

2.1.2 Using Data Types:


A variable is something that changes, or varies. In Java, a variable stores data. Data
types define the kind of data that can be stored in a variable and the limits of the data.
An example of the use of a data type is:
Char my_letter;
int no_of_albums, b, YellowRose, chair;
This code example has four variables: no_of_albums, b, YellowRose, chair. Each of
them is of type int (a reserved keyword). The default value for integers is 0. Once
again, the semicolon ends the definition. There are two major data types in Java:
reference types and primitive types.

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

2.2 Primitive Types vs. Reference Types


Data types in Java are divided into two categories—primitive types and
reference types (sometimes called non primitive types). The primitive types keywords
are (Boolean, byte, char, short, int, long, float and double).

All non primitive types are reference types, so classes, which specify the types
of objects, are reference types. A primitive-type variable can store exactly one value
of its declared type at a time. For example, an int variable can store one whole
number (such as 7) at a time. When another value is assigned to that variable, its
initial value is replaced. Primitive-type instance variables are initialized by default—
variables of types byte, char, short, int, long, float and double are initialized to 0, and
variables of type Boolean are initialized to false. You can specify your own initial
values for primitive-type variables. Recall that local variables are not initialized by
default.

2.2.1 Integer Data Types:

There are four integer data types: byte, short, int, and long. Each can handle different
ranges of numbers, as summarized in bellow:

Type Length Minimum Value Maximum Value


Byte 8-bits -128 127
Short 16-bits -32,768 32,767
Int 32-bits -21,478,483,648 214,783,647
Long 64-bits -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

2.2.2 Char data types


Type char is really a 16-bit unsigned integer that represents a Unicode value. Which
mean, it is possible to determine Unicode characters and escape codes by using a
numeric representation.

2.2.3 Floating-Point data types


Type float designates that the variable is single-precision, 32-bit, floating-point
number. Type double is a double-precision, 64-bit is floating-point number. Here
examples of declaring floating-point variables:
Float SquareFootage;
Double GrossNastionalProduct;

2.2.4 Boolean Data types


Type boolean can only have a value of true or false. Internal to Java, a Boolean value
is a 1-bit logical quality. Other programming language have Boolean value of 0 for
false and 1 for true.

2.3 Java Comments:


Comments are used in programs to improve program readability, Java have two types
of comments:
1- End-of-line (or single line) comment which is used to add a comment in a single
line for example:

// end class Main


2- Multiple-line comments which are used to add comments on multiple lines for
example:

/*
Author: Lahieb Mohammed

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

Date: 4/10/2017
Job: my first program
*/
Example:
/*
* Author: Lahieb Mohammed
* Date: 4/10/2017
* Job: my first program
*/
public class Main {
public static void main(String[] args) {
} // end method main
} // end class Main

2.4 Introduction to Variables


Variables or Identifiers are locations in the computer’s memory used to store values
by the program. Every variable has a name, a type, a size and a value.
Variables Names:
A variable name is a series of characters consisting of letters, digits, underscores (_)
and dollar signs ($) that does not begin with a digit and does not contain spaces.
Examples of valid variables:
Main _value $value number1 ahmed_account
Examples of invalid variables:
3numbers seven* nu1&nu2 button 7
2.5 Variables Types:
A variables type specifies what kind of information is stored at that location in
memory and the size required. The following table lists primitive variables types
available in Java:

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

The following table lists some variables values and their corresponding types:

2.5 Variable Declaration


Variables must be declared first before they can be used in a program, declaring
variables means specifying their names and types as shown below:
<datatype><variable name>;
Example:
1 // Program 1
2 public class Ex1 {
3 int nu1;
4 int nu2;
6

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

5 }
OR
1 // Program 2
2 public class Ex2 {
3 int nu1, nu2;
4 }
2.6 Variable Initialization
Variables can have their values assigned to them directly inside the program by the
programmer as shown below:
<datatype><variable name>=value;
Example: variable nu1 will be assigned the value 365 and variable nu2 will be
assigned the value 487
1 // Program 5
2 public class Ex5 {
3 public static void main(String[] args) {
4 int nu1, nu2;
5 nu1 = 365;
6 nu2 = 487;
// Int nu1=365;
// Int nu2=487;

7 }
8 }

Example:
2 public class Ex5 {
3 public static void main(String[] args) {
4 int nu1, nu2;
Flaot x;
5 nu1 = 365;
6 nu2 = 487;
x=(nu1+nu2)/2;
system.out.printf(“%d”,nu1);
// Int nu1=365;
// Int nu2=487;

7 }
8 }

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

2.7 Accepting Input from a User (Scanner class):


One really useful class that handles input from a user is called the Scanner class. The
The Scanner class can be found in the java.util library. To use the Scanner class, you
need to reference it in your code. This is done with the keyword import.

Import java.util.Scanner;

The import statement needs to go just above the Class statement:


Import java.util.Scanner;
Public class StringVariables {
}
The next thing you need to do is to creat an object from the Scanner class. To creat a
new Scanner object the code is:
Scanner user_input=new Scanner(System.in);

So instead of setting up an int variable or a String variable, we’re setting up a


Scanner variable. That will be called ours user_input. After an equals sign, we have
the keyword new. This is used to create our user_input object from a class. The
object we’re creating is from the scanner class. In between round brackets we have to
tell java that this will be System Input(System.in).

To get the user input, you can call into action one of the many methods available to
your new Scanner object. One of these methods is called next. This gets the next
string of text that a user types on the keyboard:
String first_name;

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

first_name=user_input.next();
we can also print some text to prompt the user:
String first_name;
System.out.print(“Enter your first name: ”);
first_name=user_input.next();

this is the same code; excep that the java will now store wherever the user types into
our family_name variable instead of our first_name variable.
To print out the input, we can add the following:
full_name=first_name+” “+family_name;
system.out.println(“you are”+full_name);
Bellow the complete java program to enter and print the first and family names:
Example:
Package Stringvars;
import java.util.Scanner;
public class void main(String[] args){
Scanner user_name = new Scanner(System.in);
String first_name;
System.out.print(“Enter your first name: ”);
first_name=user_input.next();
String family_name;
System.out.print(“Enter your family name: ”);
family_name=user_input.next();
String full_name;
full_name = first_name+ “ “+family_name;
System.out.println(“”you are”+full_name);

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

}
Output:
Enter your first name: Ahmed
Enter your family name: Mohammed
You are Ahmed Mohammed
Remark:
1. To read integer number use nextInt();
2. To read float number use: nextFloat();
3. To read Double number use: nextDouble();
4. To read string use: next();
As explain in the following example:

Example: Ask the user to read two integer numbers then find and print the sum of
these integer numbers, then ask him to read two float numbers then find and print the
subtraction of these two number.
Solution:
import jave.util.Scanner;
public class read{
public static void main(String args[])
{
float f1,f2,sub;
int n1,n2,sum;
System.out.println(“input two integer numbers”);
Scanner input= new Scanner (System.in);
n1=input.nextInt();
n2=input.nextInt();

10

Lecturer: Dr. Lahieb Mohammed Al-Yassiry


ollege of Information Engineering
Department of Computer Networks Engineering &
Department of Information and Communication Engineering
OOPII(Java), 2nd Class, 2020/2021

sum=n1+n2;
System.out.println(“the value of sum=”+sum);
System.out.println(“input two float numbers”);
f1=input.nextFloat();
f2=input.nextFloat();
sub=f1-f2;
System.out.println(“the value of subtract=”+sub);
}
}
Run:
input two integer numbers
10
20
the value of sum=30
input two float numbers
5
2
the value of subtract=3

11

Lecturer: Dr. Lahieb Mohammed Al-Yassiry

You might also like