Journey to Java: Episode 2 “Data Types”

Adam Adolfo
5 min readFeb 18, 2021

In the last “episode”, I was able to set up an environment and start a new adventure in learning Java. I have barely accomplished anything besides writing some variables and print to the terminal. The important thing is that in part 1 I was able to embark on a new learning experience and prove to myself and employers that continued learning is something that is to be expected. I plan to continue this series of blogs as I continue to learn Java, work full time, job search for my first job in tech, and contribute to my current project built in Ruby, Rails, and React.

In this blog I expect to continue to learn simple concepts and strengthen fundamentals that may be different than what I am used to in other languages.

Expressions and Variable Practice

My Udemy class has given me a little code challenge which I appreciate. It will be good practice for syntax and keeps me thinking critically and keeping me from losing attention.

The challenge is:

Make a couple variables (myFirstNum, mySecondNum, total)and create some expressions with them.

Create a new variable called myLastNum which is 1000 with total removed.

Print out the value of myLastNum.

The key is reading comprehension here as I need to find out what operator to use on what. The challenge is to find what is 1000 minus our total. This is a pretty clear sign to use a subtraction operator and subtract total from 1000 and store that value in our myLastNum variable.

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   }}

Primitave Types

The 8 primitave data types in Java are:

  • 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

This is pretty interesting as I am only familiar with boolean, int, and float from Ruby and JavaScript. Using the appropriate data types can cut down on how much memory is being allocated by your code.

Wrapper — Java uses the concept of wrapper for all 8 primitive types. Wrapper classes provide a way to use primitive data types as objects. You can then preform certain methods on those classes.

byte — Byte

short — Short

int — Integer

long — Long

float — Float

double — Double

boolean — Boolean

char — Character

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);
}
}

Casting — treat or convert a type to another data type.

If we were to declare a new variable holding a byte value and used it in an expression like this:

byte byteNum = 10;
byte myNewByteNum = byteNum / 2;

This would actually error because inside parenthesis the variable is being treated as an integer which we know the value is 10 and it fits inside a byte data type. We need to use casting to treat byteNum as a byte.

byte byteNum = 10;
byte myNewByteNum = (byte)(byteNum / 2);

A new challenge has been presented:

Create a byte variable and set it to any valid byte value.

Create a short value and set it to any valid short value.

Create a int value and set it to any valid int value.

Create a variable type of long and set it equal to 50000 plus 10 times the sum of the byte, short, and int value.

byte byteNum = 5;short shortNum = 10;int intNum = 20;long myLongNum = 50000L + 10 * (byteNum + shortNum + intNum);System.out.println(myLongNum);

Floating Point Numbers

Floating point numbers (also known as real numbers) are numbers with decimals for more precise calculations.

There is single and double precision. Precision refers to the format and space occupied by the type. Single occupies 32 bits or (4 bytes) and double occupies 64 bits or 8 bytes. It has been suggested that doubles are the better data type to program with as doubles are more accurate and compute faster at the cost of taking up more space. Floating point numbers are assumed to be doubles by java unless labeled.

Syntax for declaring floats and doubles as variables:

//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

Chars are different than strings. Chars can only store 1 character. A char takes up 2 bytes in storage. This is because a char is used to store unicode. You can look up a unicode table and find the appropriate value.

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

Boolean

True or false

boolean myBoolean = true;

Strings

Strings are not a primitave type but it is a class. It behaves like a data type so it should be included in this blog.

Strings are a series of characters. The max value of a string in memory is 2.14 billion. Strings are immutable, meaning you cannot change a string after it has been created. When you “change” them you are creating a new string and discarding the old one.

You can also interpolate strings using the addition operator.

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

Takeaways

It’s very interesting learning a new language after already know a few. I was under the impression that everything would be mostly the same with some slight syntax differences but I was wrong. There are data types I have never heard of and a strong focus on how much space they take up in memory. I know this will also be present when I get to data structures and the focus on RAM. Coming from other languages I have not seen such a keen focus on what is going on under the hood. I am interested to see how much more the learning experience differs as I continue. Thanks for reading!

--

--