DEV Community

Dhiren Serai
Dhiren Serai

Posted on

You don't know this yet - Fun Java concepts Part 2

Fun concepts according to my definition are concepts which are somewhat there in the language and is not an advanced concept but is helpful to know. I didn't knew what else to call them, these are interesting but not really advanced concepts.

Literals in Java

Confusion with Zero's ?
I have this problem, when I work with bigger numbers having lots of zero's, I get confused as to how many zero's are there in the variable.

class Literals{

public static void main(String args[]){


int number = 10_000_000;

System.out.println(number);
}

}


~/open_source/java_scripts$ java Literals 
10000000

Enter fullscreen mode Exit fullscreen mode

Notice how the number having lots of zero's is now separated with underscores making it easier for us to count how many zero's are there in the integer and also increases readability.

Upcasting and Downcasting of Datatypes

Type casting is when you assign a value of one primitive data type to another type.
Now like me you may wonder what are primitive data types?

In computer science, primitive data types are a set of basic data types from which all other data types are constructed.
Something which is the base data type of the language and is implemented in the language. When the datatype is derived then it's not primitive but rather "composite".

Most common example in Java would be char being a primitive data type and String which is derived from char is a composite data type.

Coming back to type casting :
In Java we have two types of type casting :

  • Upcasting : When the reference variable of super class refers to the object of subclass, it is known as widening or upcasting in java.

In other words, when the subclass object type is converted into superclass type, it is called widening or upcasting.

Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double

  • Downcasting : When subclass reference refers to super class object, it is called narrowing or downcasting in java. In other words, when sub class type is converted into super class type, it is called downcasting.

Narrowing Casting - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte

Casting is done by programmers a lot of times in code inorder to convert between data types, what can be missed is the order of upcasting and downcasting.

Above order shows that if we try to convert a double to integer it will lead to an error, something like :

possible lossy conversion from double to int
Enter fullscreen mode Exit fullscreen mode

Casting is a familiar concept but sometimes it's very tricky to have the order of data types in mind while casting otherwise it will lead to application errors.


Part 1 : https://dev.to/dhirensr/you-dont-know-this-yet-advanced-java-concepts-part-1-19n8

This brings us to end of the Java blog series part 2 and I hope you enjoyed reading this post.
I will continue this series of Java on different topics and concepts which I have found interesting while programming in Java.
I also share content on Twitter : DhirenSerai.
Also do let me know if you think I should document anything else that would be interesting to you.

Top comments (0)