Skip to content
Code To Live By » Java Tutorial » Variables and Types in Java

Variables and Types in Java

  • by

A variable allows us to store some sort of value and then we are able to reference it again later.

Data Types

Java is a “strongly typed” language, which means that we need to specify a data type when we declare a variable. So let’s talk about the types of data that we can store in a variable:

Primitives

“Primitive” types are considered to be basic data types that are inherently supported by the language. The main primitive data types that I want to highlight in Java are:

  • int – Store an integer (1,2,3, etc)
  • double– Store a decimal fraction (1.5, 2.75, etc)
  • boolean – Store a boolean value (true, false)

There are several other primitive data types in Java but frankly they are either different flavors of the ones above or unlikely to be useful to someone just starting out. If you would like to see the full list you may consult Oracle’s documentation on primitive types.

There’s also two other data types that I would like to introduce:

  • String – Store text (“Hello World!”)
  • LocalDateTime – Store a “DateTime” (2020-06-25T21:13:28.893968900)

Declaration vs. Initialization

Now how do we actually begin using variables? In order to use a variable we need to declare it and then initialize it. Declaring a variable is where we define its data type as well as a name. Initializing a variable is when we assign it an initial value.

So if we were to declare a String variable and initialize it with the value of “Hello World” it would look something like this:

// Declare a String typed variable with the name helloWorld
String helloWorld;
// Initialize the helloWorld to "Hello World!"
helloWorld = "Hello World!";

Or we could even combine the two which would look like this:

String helloWorld = "Hello World!";

Once you have declared and initialized your variable you can reference it later by its name.

String helloWorld = "Hello World!";
System.out.println(helloWorld);

Naming Your Variables

There’s a couple rules to keep in mind when you are deciding on a name for a variable.

A Variable Name:

  • Can contain letters, numbers, dollar signs ($), and underscores (_)
  • Must start with a letter, dollar sign ($), or underscore (_)
  • Are Case Sensitive
  • Is typically in “camelCase” where a first word is in lowercase and subsequent words are in uppercase
  • Must not be a reserved keyword
    • For example I cannot create an int variable with the name “int” as int already has special meaning in Java
    • For the full list of reserved keywords see Wikipedia’s list of Java keywords

Type Casting and Converting Between Data Types

Sometimes there will be situations where we are given objects of a certain data type but we need to convert them to something else. We can do this through something called type casting.

Perhaps we were given a double variable but we actually wanted an int and we didn’t care about the decimal fraction. In that case we could cast our double to an int.

double someDouble = 2.5;
int castToInt = (int) someDouble;

As another example, perhaps we were looking for a boolean variable but we were instead giving the values “true” or “false” as Strings. We could use something like the Boolean.parseBoolean(String) method to convert this String to a boolean.

String exampleBoolean = "true";
boolean convertedToBoolean = Boolean.parseBoolean(exampleBoolean);

Now you know the basics of creating and using variables in Java!

Leave a Reply

Your email address will not be published. Required fields are marked *