Data TypesLesson 6
Author : Mike Dane
Last Updated : October, 2017
As we write more and more complex programs in Java, we'll want to work with different types of data (data types). Maybe in one program we'll print out someone's favorite movie, in another we'll keep track of how much money a particular product costs, and maybe in another we'll ask someone if they're an organ donor. In everyday life we deal with all different types of data, so why should it be any different for programs?
Many programming languages, including Java allow you to work with and keep track of only a specific few types of data. Now that might not sound like a lot, but when you think about it, almost all data can be broken down into one of three data types.
- Text (often called strings or chars) - Names, addresses, the text on this page, etc.
- Numbers - Monetary amounts, dates, distances, etc.
- integers - whole numbers and
- floats (also called doubles) - decimal numbers
- True/False Values (often called booleans) - If someone is an Amazon Prime member, if someone is in the military, if a tv show has been cancelled, etc. This type of data may be less intuitive to a new programmer, but trust me you'll learn to love true/false logic.
Now because these are three distinct types of information, Java is going to handle them a bit differently. There will be certain things we can do with numbers that we can't do with strings or text (like division, multiplication, etc). There will also be a whole range of things we can do with boolean values (true/false) that we can't do with strings or numbers.
Anytime you're writing a program that's keeping track of different pieces of information, you always want to be aware of what data types you're using. For more specific information about how these data types work, and how to use them in Java, check out the video above where we'll dive into them more specifically!
String name = "Mike"; // String's are objects not primitives char testGrade = 'A'; // single 16-bit Unicode character. byte age0 = 0; // 8-bit signed two's complement integer short age1 = 10; // 16-bit signed two's complement integer. int age2 = 20; // 32-bit signed two's complement integer long age3 = 30L; // 64-bit optionally signed two's complement integer float gpa0 = 2.5f; // 32-bit floating point double gpa1 = 3.5; // double-precision 64-bit floating point boolean isTall; // 1 bit -> true/false isTall = true; name = "John"; System.out.println("Your name is " + name); System.out.printf("Your name is %s \n", name); /* %f -> double or float %d -> Integer %s -> string %c -> character %b -> boolean */