Java For Absolute Beginners: Lesson 1: Different Data Types
26arajesh (5)

Java For Absolute Beginners: Lesson 1: Different Data Types

Hello visitor! This is a tutorial I made for people who want to learn Java. In this tutorial, you will learn about different data types in Java. If you go on to enjoy these tutorials, I will edit this post and link new lessons that I make. Please let me know how I can improve these and if you have any questions, I will try my best to answer them (I'm not a Java pro 😀).

A Few Things About Java That You Should Know Before We Get Started

  • Basically all lines of code end with a semicolon.
  • Java is case sensitive, so make sure that certain words are capitalized and not capitalized, make sure not to add spaces in case sensitive places, and more!

Different Data Types

If you know any other programming language, you probably know these data types already. Data types or also known as variables. A variable is basically something that you can store some sort of data in, and there are different types of variables, hence the idea of "data types".

The five different data types in Java are:

  • Strings
  • Chars
  • Integers
  • Doubles
  • Booleans

All of these data types are used to store different types of data, so let's go into detail on how to declare these different variables and what they are used to store!

Strings

Strings are the first type of variables we are going to talk about! Strings are used to store multiple words. For example, if I wanted to store a greeting message that would be deployed whenever a user joined, you could store that message in a String variable.
An important thing you should know is that strings are always stored in double quotation marks (“”).

Steps to declaring a String:
1. Type the word “String” and make sure to have a capital S.
2. Put a space and type the name of your variable (it can be whatever you want it to be).
3. Put an equal sign and then put double quotation marks after the equal sign.
4. Type whatever you want in the double quotation signs.
5. Put a semicolon after the whole line and then you are done!

This is what that would look like:

String greeting = “Hello there visitor!”;
Remember, you can only use double quotation marks because single quotation marks are used for something else.

Chars

Chars are the second type of variables we are going to talk about. Chars are used to store a single character. For example, if I wanted to store the grade that a student got like an A, I could store it in a char variable.
An important thing you should know is that chars are always stored in single quotation marks (‘’).

Steps To Declaring a String:
1. Type the word “char” and make sure to have a lowercase c.
2. Put a space and type the name of your variable (it can be whatever you want it to be).
3. Put an equal sign and then put single quotation marks after the equal sign.
4. Type whatever you want in the single quotation signs.
5. Put a semicolon after the whole line and then you are done!

This is what that would look like:
char grade = ‘A’;
Remember to use single quotation marks, not double quotation marks because double quotation marks are used for strings!

Integers

Integers are the third type of variable we are going to talk about! Integers are used to store whole numbers. For example, if I wanted to store my age in a variable, I could store it in an int variable.
Integers are not stored in single or double quotations. There are no quotations used for integers.

Steps To Declaring a Integer:
1. Type the word “int” and make sure to have a lowercase i.
2. Put a space and type the name of your variable (it can be whatever you want it to be).
3. Put an equal sign after the name of your variable.
4. Type any whole number.
5. Put a semicolon after the whole line and then you are done!

This is what that would look like:
int age = 15;
Remember, there are no single or double quotation marks.

Doubles

Doubles are the fourth type of variable we are going to talk about! Doubles are used to store numbers that have decimals. For example, if I wanted to store my precise age (15 and a half years), I could store it in a double variable.
Doubles are not stored in single or double quotations, just like integers.

Steps To Declaring a Double:
1. Type the word “double” and make sure to have a lowercase d.
2. Put a space and type the name of your variable (it can be whatever you want it to be).
3. Put an equal sign after the name of your variable.
4. Type any number that has a decimal.
5. Put a semicolon after the whole line and then you are done!

This is what that would look like:
double preciseAge = 15.5
Remember, there are no single or double quotation marks.

Booleans

Booleans are the fifth type of variable we are going to talk about! Booleans are used to store either true or false. For example, if I wanted a variable that would keep track of whether a button was pressed, I could store that in a booolean.

Steps To Declaring a Boolean:
1. Type the word “boolean” and make sure to have a lowercase b
2. Put a space and type the name of your variable (it can be whatever you want it to be).
3. Put an equal sign after the name of your variable.
4. Either type true or false with a lowercase letter at the beginning.
5. Put a semicolon after the whole line and then you are done!

This is what that would look like:
boolean buttonPressed = true;
Remember, in a boolean, there is only true and false!

Printing Things Onto The Console

This does not have anything to do with data types, but this is important to know.
If you want to print something out onto the console, use the line System.out.println(“Blah blah blah”);. You can print variables as well using these as long as you get rid of the quotation marks. You can combine different things using string concatenation. This is basically when you combine different strings and variables and print them together. For example, if I wanted to print my age without just typing it manually, I can use this line of code:
int age = 15; System.out.println(“My age is ” + age);
As you can see, I used a plus sign to display two different things next to each other on the console! Make sure to put a space after a sentence if you are putting variables next too words like I did in the example above.
Another way you can print things is by using System.out.print(“Blah blah blah”); The difference is that the end now says .print instead of .println.This does the same job but however, whenever you have multiple print statements, instead of putting each one on a separate line when displayed on the console, all of the print statements are mushed onto one line.

That is all for this tutorial! I will release lesson 2 soon and will link it on here whenever I post it! If you found this helpful and if you have any questions, let me know in the comments! I am open to any constructive feedback!

Link to lesson 2: not available yet!

Thank you for following this tutorial!

You are viewing a single comment. View All
AstOwOlfo (250)

I appreciate the time that went into this post but there are some inaccuracies that I’d like to point out.

No, there are not 5 data types in Java. The best way to explain data types is to put them into the categories “primitive types” and “non-primitive types.”

Primitives would be your pre-defined data types which are built into Java and do not have methods associated with them. This would be types such as byte, short, long, float, char, etc.

Non-primitives are object references such as strings, classes, etc. They usually have methods associated with them because they are data types that were created by a developer rather than being built into Java itself, although exceptions to this rule do exist.

And with your declarations, it would’ve been nice if you showed both the primitive and non primitive variations for each variable, for example Integer and int. Also, ideally you should’ve used the identifier “D” for your initialisation double bruh = 15.5D.

With all due respect, to me, this tutorial does not sound as if it was written by someone with a confident understanding of Java and I feel like you should try to build on that if you plan on making future tutorials as your posts may confuse the people you’re trying to help.

Again, thanks a lot for the effort put into this.

26arajesh (5)

@AstOwOlfo
Hello! First of all, you are correct, Im not really that solid in Java. Second of all, thanks for the feedback! I will try my best to fix the inaccuracies some time later. I will also try to build on these.
Once again, I appreciate the constructive feedback. Have a good day!