Bootstrap FreeKB - Java - Getting Started with variables and arrays
Java - Getting Started with variables and arrays

Updated:   |  Java articles

Following are examples of different types of variables and arrays in Java.

public class Main {
    public static void main(String[] args) {
      // integers between -128 through 127. Uses less memory than short, int, and long.
      byte my_byte       = 123;

      // integers between -32768 and 32767. Uses more memory than byte, less memory than int and long.
      short my_short     = 12345;

      int my_int         = 123456789;

      // very large integers
      long my_long       = 123456789;

      // integers with decimal
      double my_double   = 1.234;

      // true or false
      boolean my_boolean = true;

      String my_string   = "Hello World";
      String[] my_array  = {"apple", "banana", "grapes", "orange"};
								
      System.out.println("my_byte    = " + my_byte);
      System.out.println("my_short   = " + my_short);
      System.out.println("my_int     = " + my_int);
      System.out.println("my_long    = " + my_long);
      System.out.println("my_double  = " + my_double);
      System.out.println("my_boolean = " + my_boolean);
      System.out.println("my_string  = " + my_string);
      System.out.println("my_array:");

      for (String item : my_array) {
          System.out.println("  " + item);
      }
   }
}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 36b7e4 in the box below so that we can be sure you are a human.