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

Lecture 2 Datatypes and Variables

Uploaded by

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

Lecture 2 Datatypes and Variables

Uploaded by

Khunsha farooq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

JAVA Programming (CST359)

Lecture 2: Datatypes and Variables

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
March 09, 2023
Variables and Data Types
• Variables are like containers that stores some values.
• Variable is the name of memory location where we store different values
depending upon the datatype of the variable.
• Datatype tells which type of value a variable can handle
• For instance, if 20 is stored in a variable “a”, then data-type of a is integer.
• Example:
int number = 8;
(Here, int is a data type, number is the variable name and 8 is the value that it
contains/stores)
Java is statically typed i.e. variables datatypes must be declared before use.
Rules for declaring a variable name
We can choose a name while declaring a Java variable if the following
rules are followed:
• Must not begin with a digit. (E.g. 1nitsri is an invalid variable)
• Name is case sensitive. (nitsri and NITSRI are different)
• Should not be a keyword (like Void, class)
• White Space not allowed. (int computer science is invalid)
• Can contain alphabets, $, _, and digits if the other conditions are met.
Data Types in Java

Data types in Java fall under the following categories:


1.Primitive Data Types
2.Non-Primitive Data Types (Derived)
Primitive Data Types
There are 8 primitive data types supported by Java:
1. byte •stores whole numbers from -128 to 127
•takes 1 byte

This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and
127.
2. short •value ranges from -(216)/2 to (216)/2 - 1
•Stores whole numbers from -32,768 to 32,767
• takes 2 byte

3. int •value ranges from -(232)/2 to (232)/2 - 1


•Stores whole numbers from -2,147,483,648 to 2,147,483,647
• takes 4 byte
• Default value: 0
4. long •value ranges from -(264) /2 to (264)/2 - 1
• Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
•takes 8 byte
This is used when int is not large enough to store the value. Note that you should end the value with an "L“.
long myNum = 15000000000L;
Int a = 10;
Byte a = 20;
Short a = 30;
System.out.println(myNum);
Primitive Data Types
5. float Datatype:
•Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
•takes 4 byte. Note that you should end the value with a “f"

6. double Datatype:
•Stores fractional numbers. Sufficient for storing 15 decimal digits
•takes 8 byte. Note that you should end the value with a “d"
Default Datatype of a Value
In case of Integral values, the default datatype of a value would be Integer
In case of Fractional Values, the default datatype of a value would be
Double

System.out.println(93); //93 will be of type Integer by default.


System.out.println(2.3); //2.3 will be of type Double by default.

To check the datatype, use the following code:


System.out.println(((Object)123.2).getClass().getSimpleName());
Use float or double?

The precision of a floating point value indicates how many digits the
value can have after the decimal point.
The precision of float is only six or seven decimal digits, while double
variables have a precision of about 15 digits.
Therefore it is safer to use double for most calculations.
Primitive Data Types
7. char Datatype
•Stores a single character/letter
•takes 2 bytes
(In other programming languages like c, char is of 1 byte only because it was holding the
ASCI values only).
But in Java, char is capable of holding the characters not limited to English Language
only.
So, instead of ASCII, UNICODE value is stored.
char charname= ‘A’; //the values must be enclosed within Single Quotes only
char charname2=“B”; //Double Quotes are used for Strings only.

8. Boolean Datatype
•value can be true or false
•1 bit
boolean b=1 //will give compile time error (incompatible types: int cannot be
converted to Boolean)
Output?
.
How to choose data types for our variables
In order to choose the data type, we first need to find the type of data,
we want to store.
After that, we need to analyse the min & max value we might use.
Strings Data type
• The String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes:
String greeting = "Hello World";
System.out.println(greeting);

A String in Java is actually a non-primitive data type.


Non-primitive data types are called reference types because they refer
to objects.
Difference between primitive and non-
primitive data types are:
• Primitive types are predefined (already defined) in Java. Non-
primitive types are created by the programmer and is not defined by
Java.
• A primitive type has always a value, while non-primitive types can be
null.
• Examples of non-primitive types are Strings, Arrays, Classes,
Interface, etc
byte myNum = 9;
float myFloatNum = 8.99f;
char myLetter = 'A’;
boolean myBool = false;
String myText = "Hello World";
Type of Variables
1. Local Variables: declared inside a function or a block like loop
2. Instance Variables: declared inside class but outside all the blocks.
Can be accessed using the objects only
3. Static Variables: can be accessed directly without objects.
4. Final Variables: value remain constant
Type of Variables
Literals
• A constant value which can be assigned to the variable is called as a
literal.
Exercise 1:
• Write a Java Program to add three numbers and storing the result in
fourth variable.
Exercise 2:
• What will be the output of following code snippet?
int a=10;
byte a=3;
System.out.println(“’value of a is ”+a);

You might also like