Skip to content

Java Variables and Types

Adrian Patterson edited this page Nov 25, 2021 · 3 revisions

Variables and Types

  • A variable (like in math) is used to represent a piece of data
    • Just as an equation uses a variable x to symbolize a number, a program uses a variable to symbolize a block of the computer's memory

    • We declare a variable by writing its type, its name, and assigning its value. E.g.

      • Here, a variable of type integer (int) named myInteger is declared and set equal to 5
      		int myInteger = 5;
      • Here, a variable of type String named myString is declared and set equal to "Hello World"
      		String myString = "Hello World!";
    • So what does the computer do with this?

      • The computer takes the values we assign, and stores them in the memory
      • Once in the memory, we can manipulate these values for calculations and much more
    • Most Common Variable Types:

       int
       float
       double
       String
       boolean

Clone this wiki locally