Types in Java
Local Variables • The programmer can store data inside a method. – Called a local variable – Disappear when the method is finished running.
• To declare a local variable: – The type – Followed by the variable name – Followed by a semicolon: int myInt;
• Variable names should be descriptive – Never pass up an opportunity to make your code more readable – Bad: a, b, dog, pete – OK: biggest, myTable, inputVal
We will study 5 major data types • • • •
int - An integer boolean - Can be true or false double - Can hold decimal numbers String - Holds a sequence of characters – Denoted by characters inside quotes (“”)
• class
Giving values to variables • Variables can be given values when declared: int myInt=6; boolean myBool=false; String myString = “Some characters”;
• Variables can be given values through an assignment statement: myDouble=1.0027; myString = “This is a string”; – Variable name, followed by equal sign, followed by value.
• Multiple variables can be declared on the same line int int1=2, int2, int3=4;
Constants • It is possible to store a value in a variable that cannot change. – Precede the declaration by the word final: final int myConst=4488; – Must initialize the variable in the declaration – Can’t ever assign a value to a final variable
• In Java a final variable is called a constant – Useful for defining a value that will be used throughout a method but will never change while that method runs.
More about Strings • Strings are typically set to a sequence of characters within double quotes • Some characters are awkward to place between double quotes – New line character: Use a backslash (\) followed by n – Double quote character: Use a backslash followed by a double quote (\”) – Backslash character: Use two backslashes (\\)
• Example, ends with a backslash, a quote, then a new line: “A string\\\”\n”
Concatenating Strings • Given two strings, can make a new string made up of the first followed by the second. • Called concatenating strings Plus sign • Use the plus sign concatenates strings String s1 = “abc”; String s2 = “def”; String s3 = s1 + s2; System.out.print(s3); //prints abcdef
• Two quotes with nothing in between (“”) is called the empty string – Concatenating a string with the empty string equals itself
Can use concatenation to break long lines • Hard to read (indentation is fouled): public static void main(String[] args){ System.out.println(“This line is too long to fit”); }
• Easier to read: public static void main(String[] args){ System.out.println(“This line is ” + “too long to fit”); }
Summary • Variables declared inside methods are called local variables. – Type, followed by variable name and semicolon – Java supports int, double, boolean, String and user defined classes
• Strings can be set to characters inside double quotes – Use backslash for special characters – Use plus sign to concatenate two strings
The Scanner • A scanner can be used to receive input from the user • In order to import the scanner use: – import java.util.*
• To create a scanner use: – Scanner – followed by the scanner name – followed by = new Scanner(System.in); Scanner input = new Scanner(System.in); •
To use: – scanner name – followed by a period – followed by the type you want to receive, and (); input.nextInt();
Examples import java.util.Scanner; public class ObjectHeight { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num; double digit; String name; time = input.nextInt(); double = input.nextDouble(); name = input.nextString(); } }