1 / 9

Types in Java

Types in Java. Computer Science 3 Gerb Objective: Understand type declarations 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

Download Presentation

Types in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Types in Java Computer Science 3 Gerb Objective: Understand type declarations in Java

  2. 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

  3. 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

  4. 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;

  5. 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.

  6. 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”

  7. Plus sign concatenates strings Concatenating Strings • Given two strings, can make a new string made up of the first followed by the second. • Called concatenating strings • Use the plus sign 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

  8. 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”); }

  9. 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

More Related