Java Basic 2.1 Data Representation in Java: Literals Are Entered Explicitly Into The Code
Java Basic 2.1 Data Representation in Java: Literals Are Entered Explicitly Into The Code
Java Basic 2.1 Data Representation in Java: Literals Are Entered Explicitly Into The Code
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”.
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.
There are four integer data types: byte, short, int, and long. Each can handle different
ranges of numbers, as summarized in bellow:
/*
Author: Lahieb Mohammed
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
The following table lists some variables values and their corresponding types:
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 }
Import java.util.Scanner;
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;
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);
}
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
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