BAP - Lec2Data Types and Variable
BAP - Lec2Data Types and Variable
Programming
9,223,372,036,854,775,808 to
long 8 bytes
9,223,372,036,854,755,807
Integer
Integer
• The range of values is calculated as −(2n−1) to
(2n−1)−1; where n is the number of bits
required.
• For example, the byte data type requires 1
byte = 8 bits. Therefore, the range of values
that can be stored in the byte data type is
−(28−1) to (28−1)−1
= −27 to (27) -1
= −128 to 127
Floating Point
• Floating point data types are used to represent
numbers with a fractional part.
• Single precision floating point numbers occupy 4
bytes and Double precision floating point
numbers occupy 8 bytes.
• There are two subtypes:
Range of values
Type Size that can be
stored
3.4e−038 to
float 4 bytes
3.4e+038
1.7e−308 to
double 8 bytes
1.7e+038
Character
• It stores character constants in the memory.
• It assumes a size of 2 bytes, but basically it can
hold only a single character because char stores
unicode character sets.
• It has a minimum value of ‘u0000’ (or 0) and a
maximum value of ‘uffff’ (or 65,535,
inclusive).
Boolean
• Boolean data types are used to store values
with two states:
– true or false.
Java Variables
• What is a Variable?
– A variable can be thought of as a container
which holds values for you, during the life of a
Java program.
– A variable is a name of a reserved area
allocated in memory. In other words, it is a
name of memory location.
– Every variable is assigned a data type which
designates the type and quantity of value it can
hold.
Java Variables
• Fields in classes are also variables.
Remember the bicycle class fields: cadence,
speed and gear
• In order to use a variable in a program you
need to perform 2 steps
– Variable Declaration
– Variable Initialization
Java Variables
• Variable Declaration
– To declare a variable, you must specify the data
type & give the variable a unique name
Java Variables
• Variable Declaration
– Examples of other Valid Declarations are:
• int a,b,c;
• float pi;
• double d;
• char a;
Java Variables
• Variable Initialization
– To initialize a variable, you must assign it a
valid value.
Java Variables
• Variable Initialization
– Example of other Valid Initializations are
• int a=2,b=4,c=6;
• pi =3.14f;
• do =20.22d;
• a=’v’;
Java Variables
• Variable Initialization
– Examples of other Valid Initializations are
• Numbers
– To declare and assign a number use the following syntax:
int myNumber;
myNumber = 5;
– Or you can combine them:
int myNumber = 5;
Java Variables
• Variable Initialization
– Examples of other Valid Initializations are
• Numbers
– To define a double floating point number, use the following
syntax:
double d = 4.5;
d = 3.0;
– If you want to use float, you will have to cast:
float f = (float) 4.5;
Java Variables
• Variable Initialization
– Examples of other Valid Initializations are
• Characters and Strings
char c = 'g';
– String is not a primitive. It's a real type, but Java has special
treatment for String.
– Here are some ways to use a string:
}
Example how to define a
constant:
public class Constant1 {
public void Method1() {
final double e = 2.718281828;
// e = 1.0; // Try to change e - forbidden if e is "final".
}
public void Method2() {
final int c = 299792458;
}
public final double Pi = 3.1415926535;
}
Packaging Constants
• If your program uses many constants, then it
would be a good idea to put all the constants in
one place.
• In Java, a class is uses to hold:
– Constructors
– Methods
– Variables
– Constants
• You may want write a class that only holds
constants.
Packaging Constants
public class MyConstants {
public static final double PI = 3.1415926535;
public static final double E = 2.718281828;
}
Packaging Constants
public class UseConstants {
public static void main(String[] args) {
// TODO code application logic here
double r = 10;
double area;
area = MyConstants.PI * r * r;
System.out.print("radius = ");
System.out.println(r);
System.out.print("area = ");
System.out.println(area);
}
}
Java's Math class
• Java has provided (i.e., packaged) the above
constants in their API.
• The class that contains these Mathematical
constants is called: Math
• We can rewrite the above program using the
constants in Java's API as follows: ( see
next slide)
Java's Math class
public class UseConstants {
public static void main(String[] args) {
// TODO code application logic here
double r = 10;
double area;
area = Math.PI * r * r;
System.out.print("radius = ");
System.out.println(r);
System.out.print("area = ");
System.out.println(area);
}
}
Input from the keyboard
• In Java, there are many ways to read strings
from input.
• The simplest one is to make use of the class
Scanner, which is part of the java.util library.
• Using this class, we can create an object to
read input from the standard input channel
System.in (typically, the keyboard), as follows:
Scanner scanner = new Scanner(System.in);
Input from the keyboard
• Then, we can use the nextLine() method of the
Scanner class to get from standard input the next
line (a sequence of characters delimited by a
newline character), according to the following
schema:
import java.util.Scanner;
public class KeyboardInput {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine();
System.out.println(inputString);
}
}
Input from the keyboard
• Explanation:
– import java.util.Scanner; - imports the class Scanner from
the library java.util
– Scanner scanner = new Scanner(System.in); - creates a
new Scanner object, that is connected to standard input
(the keyboard)
– String inputString = scanner.nextLine(); temporarily
suspends the execution of the program, waiting for input
from the keyboard;
– the input is accepted as soon as the user digits a carriage
return;
– (a reference to) the string entered by the user (up to the
first newline) is returned by the nextLine() method;
– (the reference to) the string is assigned to the variable
inputString.
Input from the keyboard
• We can also read in a single word (i.e., a
sequence of characters delimited by a
whitespace character) by making use of the
next() method of the Scanner class.
• The example shown on the next slide, on Java
Keyboard Input, takes from the Student name,
circle radius and rectangle height and length.
Input from the keyboard
import java.util.Scanner;
public class StringExample {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
System.out.println("Enter Student Name:");
String stdName = scan1.nextLine();
System.out.println("Enter Circle Radius as a whole number:");
int radius = scan1.nextInt();
System.out.println("Enter Rectangle length as a floating point value:");
double length = scan1.nextDouble();
System.err.println("Enter Rectangle width as a floating point value:");
double width = scan1.nextDouble();
//Display the results of computation
System.out.println("\nStudent Name "+ stdName);
System.out.println("Circle Perimetre: " + 2*Math.PI*radius);
System.out.println("Rectangle Area: " + width*length);
}
}
Input from the keyboard
• The Java Scanner class breaks the input into
tokens using a delimiter that is whitespace
bydefault.
• It provides many methods to read and parse
various primitive values.
• Java Scanner class is widely used to parse text
for string and primitive types using regular
expressions.
• Java Scanner class extends Object class and
implements Iterator and Closeable interfaces.
Commonly used methods of Scanner class
Method Description
public String next() it returns the next token from the scanner.
it moves the scanner position to the next line and returns the value as
public String nextLine()
a string.
public double
it scans the next token as a double value.
nextDouble()
Java Scanner Example with delimiter
import java.util.*;
public class ScannerTest2{
public static void main(String args[]){
String input = "10 tea 20 coffee 30 tea buiscuits";
Scanner s = new Scanner(input).useDelimiter("\\s");
//System.out.println(s.nextInt());
//System.out.println(s.next());
//System.out.println(s.nextInt());
//System.out.println(s.next());
while (s.hasNextInt()|| s.hasNext())
{
if(s.hasNextInt())
System.out.println(s.nextInt());
else
System.out.println(s.next());
}
s.close();
}
}