Java Fundamentals – Part 1

Hello guys, this is the beginning of a new article series about Java language fundamentals. I am going to skip the history of Java part and the basic process since it is going to be boring to read for most of you. I am going to start things off by discussing Variables and Operators in Java.


Numerical Variables

Numerical variables can be divided into two sub-sections.

1. Whole Numbers

These are known as integers in maths. I am listing down some examples to make the things clearer.

  • Number of copies sold of a book
  • Number of students in a school
  • Number of birds sitting on a tree

2. Decimal Point Numbers

These are called real numbers in maths. Here are some examples.

  • Weight of your pizza in grams
  • Your height in meters
  • Length of a road in centimeters

How to decide what to choose?

Range of int is from -2,147,483,648 to 2,147,483,647

Range of float is approximately ±3.40282347E+38F (6-7 significant decimal digits)

The common advice is if you are not sure whether to use which data type, go with double data type. This gives you precision up to 15 digits.

What does 34.56f mean?

Here the f means that it is a float number. If you remove the f, you will get a double.


Rules for Declaring Variables

All of these are not rules. Some of these are best practices. So, I recommend you guys to follow these rules when naming a variable in Java.

  • Must start with a letter (A-Z, a-z)
  • Can contain Underscore (‘_’) and Dollar Sign (‘$’)
  • Can be of any length
  • Java is case-sensitive
  • No whitespaces
  • Use Lower Camel Case (Ex:- thisIsAnExample)

Image Source – Wikipedia

Assigning Variables

We use equals (“=”) mark to assign values to variables.

Correct ways to declare and initialize variables in Java

int a, b, c;
int a = 10, b, c = 15;
int a = b = c = 10;

Arithmetic Operators

  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Addition (+)
  • Substraction (-)
  • Increment (++)
  • Decrement (–)

Relational Operators

  • Equals to (==)
  • Not equals to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Logical Operators

  • Logical AND / Short Circuit AND (&&)
  • Logical OR / Short Circuit OR (||)
  • Logical NOT (!)

Type Conversions

This concept is simple. But most students make this harder than it is. There are only 3 points that you guys should remember. Forget all fancy names. Just remember these points.

1. A data type like int can be converted into float without any additional programming. No data loss is going to happen.

int small = 45;
long big = small; //No Data Loss

2. float values can be converted into int using a cast. But here we are going to have a data loss.

float big = 3.98f
int small = (int) big; //small = 3

3. When an operator handles a mix of int and float values, any integers are temporarily converted into float for calculation purposes.

float big = (float) (15 + 16) / 2; //big becomes 15.5

This ends my first Java Fundamental article. I hope you guys understood what I discussed. I always try to be short and clear in my article to make it easier for you guys. If you liked the article, please share it among your friends. And don’t forget to push the subscribe button to receive a notification when I publish a new article. Stay safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: