02 Var Data1
02 Var Data1
1
In this lecture, you will learn…
• What a variable is
– Types of variables
– Naming of variables
– Variable assignment
• What a primitive data type is
• Other data types (ex. String)
2
What is a Variable?
• In basic algebra, variables are symbols
that can represent values in formulas.
• Formula: y=x2+2
can represent any number value.
3
A Variable Analogy
• Think of variables as an empty box that you
can put values in.
We can label the box with a name like “Box a” and re-use it many times.
5
Java Types
• Integer Types:
– int: Most numbers you’ll deal with.
– long: Big integers; science, finance, computing.
– short: Small integers. Legacy. Not very useful.
– byte: Very small integers, useful for generic data.
• Floating Point (Decimal) Types:
– float: Single-precision decimal numbers
– double: Double-precision decimal numbers.
• Other Types:
– String: Text strings.
– boolean: True or false.
– char: Latin Alphanumeric Characters
6
Declaring Variables in Java
• Variables are created by declaring their
type and their name as follows:
– type name;
• Declaring an integer named “x” :
– int x;
• Declaring a string named “greeting”:
– String greeting;
• We have not assigned values to these
variables; just made empty boxes.
8
Assigning Values to Variables
• Assign values to variables using the syntax:
– String name = "value";
• For example:
– int x = 100;
– String greeting = “Jambo”;
• Illegal to assign a variable the wrong type:
– int x = “Jambo”;
– int x = 1.2;
– String greeting = 123;
• Can declare and assign in one step:
– int x = 100;
– String greeting = “Jambo”;
9
Naming Variables
• Variable names (or identifiers) may be any
length, but must start with:
– A letter (a – z),
– Or, an underscore ( _ ).
11
Floating Point Types
• Initialize doubles as you would write a
decimal number:
– double y = 1.23;
• Use a trailing ‘d’ to force a value to be
double:
– double y = 1d/3; // y
= .333333333
– double z = 1/3; // z = 0.0 …
Why?
12
public class AdditionOperator
{
public static void main(String[] args)
{
int firstVal = 5;
int secondVal = 2;
System.out.println(firstPlusSecond);
}
}
Integer Division
in Java
5/2=2
5 / 2.0 = 2.5
public class TrickyJava
{
public static void main(String[] args)
{
int candyBars = 20;
int friends = 5;
friends = friends + 1;
System.out.print("Candy bars: ");
System.out.println(candyBars);
System.out.print("Friends: ");
System.out.println(friends);
candyBarsPerPerson = candyBars / friends;
System.out.print("Candy per person: ");
System.out.println(candyBarsPerPerson);
}
}
public class TrickyJava
{
public static void main(String[] args)
{
int candyBars = 20;
int friends = 5;
friends = friends + 1;
System.out.print("Candy bars: ");
System.out.println(candyBars);
System.out.print("Friends: ");
System.out.println(friends);
candyBarsPerPerson = candyBars / friends;
System.out.print("Candy per person: ");
System.out.println(candyBarsPerPerson);
}
}
public class MyProgram
{
public static void main(String[] args)
{
int intTempF = 70;
System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
}
}
public class IncreaseDecrease
{
public static void main(String args[])
{
int num1 = 4;
int num2 = 3;
System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
}
}
Casting and Range of Variables
Compile-time Error
5.0 5.0
2.5
2.0
2.5
public class CastingOrderOfOperations
{
public static void main(String[] args)
{
int doubleCastedToInt = (int) 10.9 ;
System.out.print("(int) 10.9 = ");
System.out.println(doubleCastedToInt);
double castNeither = 2 / 3;
System.out.print("2 / 3 = ");
System.out.println(castNeither);
double castNeither = 2 / 3;
System.out.print("2 / 3 = ");
System.out.println(castNeither);
double pow =
Integer.MAX_ VALUE
Math.pow(num,3);
double sqrt =
Math.sqrt(81.0);
double ran =
The minimium integer value in Java is -
2147483648
The maximum integer value in JavaMath.random();//[0,1)
is 2147483647
Boolean Type
• The values true or false are case-
sensitive keywords.
• Example:
– boolean monsterHungry = true;
– boolean fileOpen = false;
29
String Type
• Strings are not a primitive. They are what’s
called an Object, which we will discuss later.
31