Journey to Java: Episode 2 “Data Types”

public class Practice {   public static void main(String[] args) {      int myFirstNum = 500;      int mySecondNum = 200;      int total = myFirstNum + mySecondNum; //700      int myLastNum = 1000 - total;      System.out.println(myLastNum);      //prints 300   }}
  • Boolean — True/False.
  • Byte — Stores whole numbers from -128 to 127.
  • Short — Stores whole numbers from -32,768 to 32,767.
  • Int — Stores whole numbers from -2,147,483,648 to 2,147,483,647.
  • Long — Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • Float — Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits.
  • Double — Stores fractional numbers. Sufficient for storing 15 decimal digits.
  • Char — Stores a single character/letter or ASCII values
public class Main {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
byte byteNum = 10;
byte myNewByteNum = byteNum / 2;
byte byteNum = 10;
byte myNewByteNum = (byte)(byteNum / 2);
byte byteNum = 5;short shortNum = 10;int intNum = 20;long myLongNum = 50000L + 10 * (byteNum + shortNum + intNum);System.out.println(myLongNum);

Floating Point Numbers

//float
float myFloat = 5.25f;
//double
double myDouble = 5.25d;
//without using d or f after the declaration java will assume it is a double.

Char and Boolean

char myChar = '\u0040';
//myChar's value will be the letter 'D'
boolean myBoolean = true;

Strings

String myString = "Adam";
System.out.println("Hello, " + myString );

Takeaways

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store