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

MIT11053 Chapter 02 2023

Uploaded by

Shibly Hasan
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)
16 views

MIT11053 Chapter 02 2023

Uploaded by

Shibly Hasan
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
You are on page 1/ 33

Chapter 02

Java Data Types and


Taking Inputs
MIT11053, Fundamentals of Programming
By:
Prof. S. Sabraz Nawaz
Professor in MIT
Department of MIT, SEUSL
Topics Covered

• Statements
• Variables
• Data Types
• Arithmetic Calculations
• Taking Input from User

MIT11053 by Prof. S.Sabraz Nawaz 2


Statements

• A Statement is the simplest task you can accomplish in Java.

int othrs=5;
System.out.println("netsalary= "+netsal);

You need to put a semi colon ; at the


end of a statement.
MIT11053 by Prof. S.Sabraz Nawaz 3
Variables

• Variables are locations in memory where values can be


stored

MIT11053 by Prof. S.Sabraz Nawaz 4


Variable Name

• Variable is a location in memory


• Each location in memory has a memory
address, which is a number
o This long number is inconvenient to use when
we want to access the memory location
• We give a human understandable name to
refer to this number
o e.g. age, quantity
• The compiler and the interpreter maps this
name to the memory address number

MIT11053 by Prof. S.Sabraz Nawaz 5


Value of a Variable

• At a given time one value can be stored under the variable

quantity

MIT11053 by Prof. S.Sabraz Nawaz 6


Variable Type

• You need to specify what type of data is to be stored. e.g.


int, char
• This is because we must instruct how much memory
should be reserved by the program to store the value of a
variable
• The amount of memory needed depends on the maximum
of the value we need to store in the variable.

MIT11053 by Prof. S.Sabraz Nawaz 7


Variable Type…

MIT11053 by Prof. S.Sabraz Nawaz 8


Java Data Types
• Java supports eight primitive data types.
• These are built into the language itself.
• Consists of Numeric Types, char type and Boolean type.
• Remember String is not a primitive data type in Java
o String is a class in Java, thus it is handled as a data type derived from
a class.
• In Java we write classes and class can be a data type
o Eg: If you write a class called Student you can use it as the Student
data type

MIT11053 by Prof. S.Sabraz Nawaz 9


Data Types

Name Range Storage Size

byte –27 (-128) to 27–1 (127) 8-bit signed

short –215 (-32768) to 215–1 (32767) 16-bit signed

int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed

long –263 to 263–1 64-bit signed


(i.e., -9223372036854775808
to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308

MIT11053 by Prof. S.Sabraz Nawaz 10


Declaring Variables

int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;

MIT11053 by Prof. S.Sabraz Nawaz 11


Declaring Variables

public static void main (String args[]) {


int count;
String title;
boolean isAsleep;
...
}

Variables are usually defined at the


beginning. However this need not always
be the case.
MIT11053 by Prof. S.Sabraz Nawaz 12
Declaring Variables

int x, y, z;
String firstName, lastName;

Multiple variables can be defined under


one type

MIT11053 by Prof. S.Sabraz Nawaz 13


Declaring Variables

• Once declared the variable need to be initialized


o Initialization – Specify the value we want to store in the variable

int myAge;
myAge = 43;
String myName;
myName = “SaNa";

MIT11053 by Prof. S.Sabraz Nawaz 14


Declaring and Initializing
in One Step
• int x = 1;
• double d = 1.4;

int age=19; int age;



age = 19;
You can also initialize variables as
the declaration is done.
The above statements are identical.
MIT11053 by Prof. S.Sabraz Nawaz 15
Variable Names

int age;
float $money;
char my_char;
long _no;
String name7;
A Variable Name should start with an
Alphabetical letter or $, or _ symbol
The other characters can include numbers
But you cannot use symbols like @, #, etc
MIT11053 by Prof. S.Sabraz Nawaz 16
Variable Names

int my age;
float @money;
char 6my_char;
long no*;

The above names are incorrect.


You cannot have spaces and other
special symbols.

MIT11053 by Prof. S.Sabraz Nawaz 17


Variable Names

int qty;
String firstName;
float basicSal, netSal;

It’s best if you can give suitable


(short but meaningful) variable names.

MIT11053 by Prof. S.Sabraz Nawaz 18


Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2

MIT11053 by Prof. S.Sabraz Nawaz 19


How to Evaluate an Expression
• Though Java has its own way to evaluate an expression behind the
scene, the result of a Java expression and its corresponding
arithmetic expression are the same. Therefore, you can safely apply
the arithmetic rule for evaluating a Java expression.
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53

MIT11053 by Prof. S.Sabraz Nawaz 20


The String Type

The char type only represents one character. To represent a string


of characters, use the data type called String. For example,

String message = "Welcome to Java";

String is actually a predefined class in the Java library just like the
System class. The String type is not a primitive type. It is known
as a reference type. Any Java class can be used as a reference type
for a variable.

MIT11053 by Prof. S.Sabraz Nawaz 21


String Concatenation

// Three strings are concatenated


String message = "Welcome " + "to " + "Java";

// String Chapter is concatenated with number 2


String s = "Chapter" + 2; // s becomes Chapter2

// String Supplement is concatenated with character B


String s1 = "Supplement" + 'B'; // s1 becomes SupplementB

MIT11053 by Prof. S.Sabraz Nawaz 22


Taking User Inputs

MIT11053 by Prof. S.Sabraz Nawaz 23


Using Scanner Class

• The Scanner class is a class in java.util, which allows the user


to read values of various types.
• The Scanner looks for tokens in the input. A token is a series of
characters that ends with what Java calls whitespace. A
whitespace character can be a blank, a tab character, a carriage
return, or the end of the file.

MIT11053 by Prof. S.Sabraz Nawaz 24


Using Scanner Class
Method Returns
int nextInt() Returns the next token as an int.

long nextLong() Returns the next token as a long.

float nextFloat() Returns the next token as a float.

double nextDouble() Returns the next token as a double.

Finds and returns the next complete token from this


String next() scanner and returns it as a string; a token is usually
ended by whitespace such as a blank or line break.

String nextLine() Returns the rest of the current line, excluding any line
separator at the end.

MIT11053 by Prof. S.Sabraz Nawaz 25


Using Scanner Class

01

02

03

MIT11053 by Prof. S.Sabraz Nawaz 26


Exercise

• A university pays its Academic Staff


o Academic Allowance 39% of Basic Salary
o Research Allowance 45% of Basic Salary
o Cost of Living Allowance 7,550/=.
• And deducts UPF 8% of the Basic Salary.
• Write a Java program to input the Basic Salary.
Calculate the above and display them all with
Net Salary

MIT11053 by Prof. S.Sabraz Nawaz 27


Shortcut Assignment Operators

Operator Example Equivalent


+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8

MIT11053 by Prof. S.Sabraz Nawaz 28


Numeric Type Conversion

Consider the following statements:


byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

MIT11053 by Prof. S.Sabraz Nawaz 29


Conversion Rules

When performing a binary operation involving two operands


of different types, Java automatically converts the operand
based on the following rules:

1. If one of the operands is double, the other is converted into


double.
2. Otherwise, if one of the operands is float, the other is converted
into float.
3. Otherwise, if one of the operands is long, the other is converted
into long.
4. Otherwise, both operands are converted into int.

MIT11053 by Prof. S.Sabraz Nawaz 30


Type Casting

Implicit casting
double d = 3; (type widening)

Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)
range increases

byte, short, int, long, float, double

MIT11053 by Prof. S.Sabraz Nawaz 31


Exercises

1. Write a Java program that takes three marks of an exam and displays the
total and the average.
2. Write a program to input how many notes, coins of denominations of
1000/=, 500/=, 200/=, 100/= 50/=,20/=,10/=,5/=, 2/= and 1/= are
available. Print the total amount

MIT11053 by Prof. S.Sabraz Nawaz 32


End of Lecture

You might also like