0% found this document useful (0 votes)
25 views26 pages

Java Variables and Data Types Guide

Here is a program that takes runtime input from the user and performs basic arithmetic calculations: import java.util.Scanner; public class ArithmeticCalculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = input.nextInt(); System.out.print("Enter second number: "); int num2 = input.nextInt(); System.out.print("Enter an operator (+, -, *, /): "); String operator = input.next(); int result = 0; if(operator.equals("+")) { result = num1 + num2;

Uploaded by

unkkggo104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views26 pages

Java Variables and Data Types Guide

Here is a program that takes runtime input from the user and performs basic arithmetic calculations: import java.util.Scanner; public class ArithmeticCalculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = input.nextInt(); System.out.print("Enter second number: "); int num2 = input.nextInt(); System.out.print("Enter an operator (+, -, *, /): "); String operator = input.next(); int result = 0; if(operator.equals("+")) { result = num1 + num2;

Uploaded by

unkkggo104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lecture No.

3
Variables and Data Types
Agenda
1. Variable
2. DataTypes
3. Type Casting
4. Runtime Input
Data Types in Java:
Data types specify the different sizes and values that can
be stored in the variable. There are two types of data types
in Java:

Primitive data types: The primitive data types include


boolean, char, byte, short, int, long, float and double.

Non-primitive data types: The non-primitive data types


include Classes, Interfaces, and Arrays.
Difference between Primitive and Non Prmitive
data types
1. Primitive Data Types: A primitive data type is
predefined by the programming language. The
size and type of variable values are specified,
and it has not additional methods.

2. Non-Primitive Data Types: These data types are


not actually defined by the programming
language but are created by the programmer
ExampleGet y
our own Java Serve
int myNum = 5; // Integer (whole number)

float myFloatNum = 5.99f; // Floating point number

char myLetter = 'D'; // Character

boolean myBool = true; // Boolean

String myText = "Hello";


Java Numbers Java Numbers
Primitive number types are divided into two groups
Integer types stores whole numbers, positive o
negative (such as 123 or -456), without decimal
Valid types are byte, short, int and long. Whic
type you should use, depends on the numeric value
Floating point types represents numbers with
fractional part, containing one or more decimal
There are two types: float and double.
Even though there are many numeric types in Java
the most used for numbers are int (for who
numbers) and double (for floating point numbers
However, we will describe them all as you continu
to read.
Byte
The byte data type can store whole numbers
from -128 to 127. 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:

byte myNum = 100; [Link](myNum);


Short
The short data type can store whole numbers from -32768 to 32767:
Example
short myNum = 5000; [Link](myNum);

Int
The int data type can store whole numbers from -2147483648 to
2147483647.
In general, and in our tutorial, the int data type is the preferred data
type when
we create variables with a numeric value.
Example
int myNum = 100000; [Link](myNum);

Long
The long data type can store whole numbers from -
9223372036854775808 to
9223372036854775807. This is used when int is not large enough to
store the value.
Note that you should end the value with an "L":
Example
long myNum = 15000000000L; [Link](myNum);
Floating Point Types
You should use a floating point type whenever you need a number with a
decimal,
such as 9.99 or 3.14515.
The float and double data types can store fractional numbers.
Note that you should end the value with an "f" for floats and "d" for
doubles:
Float Example
float myNum = 5.75f; [Link](myNum);

Double Example
double myNum = 19.99d; [Link](myNum);
Boolean Types
Very often in programming, you will need a data type that can only
have one of two values, like:
•YES / NO
•ON / OFF
•TRUE / FALSE
For this, Java has a boolean data type, which can only take the
values true or false:

ExampleGet your own Java Ser


boolean isJavaFun = true; boolean isFishTasty = false;
[Link](isJavaFun);
// Outputs true [Link](isFishTasty); // Outputs false
Java Type Casting
Type casting is when you assign a value of one primitive data
type to another type. The type casting is the way to converting
data from one data type to another data [Link] using casting,
data cannot be changed but the data type is changed.

In Java, there are two types of casting:


What are the benefits of type casting?
It helps you in converting one data type to another data type. It helps in
making the program lightweight. With this, you can take advantage of
features like type representations and hierarchies.

•Widening Casting (automatically) - converting a smaller type


to a larger type size
byte -> short -> char -> int -> long -> float -> double
•Narrowing Casting (manually) - converting a larger type to a
smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when passing a
smaller size type to a larger size type:

ExampleGet your own Java Serv


public class Main
{
public static void main(String[] args)
{
int myInt = 9; double myDouble = myInt;
// Automatic casting: int to double
[Link](myInt);
// Outputs 9
[Link](myDouble);
// Outputs 9.0
}
}
Narrowing Casting
Narrowing casting must be done manually by placing the type in
parentheses in front of the value:

Example
public class Main
{
public static void main(String[] args)
{
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int
[Link](myDouble); // Outputs 9.78
[Link](myInt); // Outputs 9
}
}
Java Variables
A variable is a container which holds
the value while the Java program is
executed. A variable is assigned with
a data type.
Variable is a name of memory
location.
There are three types of variables in
java: local, instance and static.

A variable is the name of a reserved


area allocated in memory. In other
words, it is a name of the memory
location. It is a combination of "vary
+ able" which means its value can
be changed.
Java Variable Example: Add Two Numbers

class Simple
{
public static void main(String[] args)
{
int a=10;
int b=10;
int c=a+b;
[Link](c);
}
}
How to get input from user in Java
Java Scanner Class
Java Scanner class allows the user to take input from the
console. It belongs to [Link] package.
It is used to read the input of primitive types like int, double,
long, short, float, and byte. It is the easiest way to read input
in Java program.
Scanner sc=new Scanner([Link]);
The above statement creates a constructor of the Scanner
class having [Link] as an argument. It means it is going
to read from the standard input stream of the program.
The [Link] package should be import while using Scanner
class.
It also converts the Bytes (from the input stream) into
Methods of Java Scanner Class
Java Scanner class provides the following methods to read different primitives types:
Method Description
int nextInt() It is used to scan the next token of the input as an integer.

float nextFloat() It is used to scan the next token of the input as a float.

double nextDouble() It is used to scan the next token of the input as a double.

byte nextByte() It is used to scan the next token of the input as a byte.

String nextLine() Advances this scanner past the current line.

boolean nextBoolean() It is used to scan the next token of the input into a boolean value.

long nextLong() It is used to scan the next token of the input as a long.

short nextShort() It is used to scan the next token of the input as a Short.

BigInteger It is used to scan the next token of the input as a BigInteger.


Example of integer input from user
The following example allows user to read an integer form the
[Link].
[Link] [Link].*;
[Link] UserInputDemo {
[Link] static void main(String[] args) {
[Link] sc= new Scanner([Link]); //
[Link] is a standard input stream
[Link]("Enter first number- ");
[Link] a= [Link]();
[Link]("Enter second number- ");
[Link] b= [Link]();
[Link]("Enter third number- ");
[Link] c= [Link]();
[Link] d=a+b+c;
[Link]("Total= " +d);
13.} }
A simple program of String runtime input

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);
// Create a Scanner object
[Link]("Enter username");
String userName = [Link]();
// Read user input
[Link]("Username is: " + userName);
// Output user input }}
Exercise:
Write a program java that
should take runtime input
and perform all
arithmetic calculation
operations.

You might also like