ROAD TO ANDROID DEVELOPER-Season 1-JAVA-Variables and Datatypes

Tharani balan SK
7 min readAug 28, 2023

--

Hi all, welcome back, In our last article we saw a basic intro on android development , Java and we also wrote a “Hello world” code. If you don’t know what I am talking about or what this “intro” thing is about…then please do check my previous article, I have left the link below….scroll down to check it out. Before getting into today’s topic, I forgot to tell you about another thing about Java….there are some stuffs known as JVM, JDK and JRE. Eventhough you won’t need it you just might want to know about it as these are some properties of Java.

JVM : JVM stands for Java Virtual Machine. The thing that happens when you run your java code is, the compiler first converts it into bytecode and then the JVM will convert it further into a machine code(for your machine to understand your code)

JRE : JRE stands for Java Runtime Environment and it consists of the JVM , certain class libraries and other components that are needed to run your Java programs.

JDK : JDK stands for Java Development Kit. It consists of the JRE, compilers, debuggers etc. If you are going to run just programs then JRE is more than enough but if you are going into software development kinda stuffs then you need JDK .

Src : https://www.programiz.com/java-programming/jvm-jre-jdk

For attending Java related interviews, knowing this much about how Java functions, is more than enough. You are free to explore further on google if you want to know more about it as we are not going to cover them here, as i had mentioned already, in my previous article my focus is fully on explaining only about the concepts that come under android development and nothing else….so without further delay, let’s get into our today’s topic,

VARIABLES :

Variables are basically like naming something for example, let’s say you are a newborn kid and your parents forgot to name you for whatever reason(Just kidding) and so everybody begin to call you just “boy”/ “girl” now let’s say one day you are playing with your friends and your mom comes to pick you and so she calls you “Hey boy/girl it’s time, let’s go home!!” now what will happen….remember you are not the only boy/girl down there on the playground, so obviously every boy/girl responds to the call right? so inorder to avoid this confusion every one of us has been assigned a name similarly in programming, variables are something (it just can be anything even something crazy like “nhdjfhdsjll” as long as it is understandable to you and the reader) which is used to store a data. For eg,

int a = 5;
char alphabet = "a";

In here, we are naming our “5” as “a” i.e. we are storing the value 5 inside a variable “a” similarly we are storing an English letter, “a” inside a variable “alphabet”. You can give any variable name as you wish as long as you follow certain constraints, they are,

  • Remember that most programming languages out there are case sensitive i.e. the variable name “a” and another variable name “A” are not the same. Though in English, both mean the same, When it comes to programming these two are different. For eg,
public class variables{
public static void main(String[] args){
int a = 20; //value of the variable "a" is 20 here
int A = 20; //value of 'another' variable "A" is again 20
System.out.print(a); //printing the value of 'a' so output will be 20
System.out.print(A); //same output 20
//Now let's change the value of the variable "A" to '30'
int A = 30;
System.out.print(A); //we are printing the value of "A" so output will be 30
// Now tell me the output of below line
System.out.print(a); /*will it be 20 or 30? It will be the same 20 why?
* because java is case-sensitive. Try it out yourself */
}
}
  • Variable names cannot start with a number but it can be started with a letter, an underscore(_) or a dollar sign($)
int num = 5; //correct
int _num = 5; //correct
int 1num = 5; //wrong
int $num = 5; //correct
int num1 = 5; //correct
  • variable names cannot contain a whitespace(gaps) between them

Understanding this much about variables is enough. As you explore further you will understand how to use variable names in a professional manner and why, though certain rules such as starting with an underscore or dollar sign is applicable, people don’t use them etc.

DATATYPES :

Datatypes are something which tells what type of values does a variable can store for example,

int num = 8 //here "int" is a datatype i.e. we are saying that the variable "num"
//belongs to an integer type or in other words we are declaring that
//the variable "num" can store only integer numbers

The most commonly used primitive Datatypes that are available in Java are, (There are actually 8 I guess….but we are going to see only the commonly used ones) There is another type of datatype known as object type which is user-defined or can be imported from libraries which we will talk about later.

  • int :Any variable declared with this datatype can store integer values ranging from -2³¹ to 2³¹-1.
public class datatypes{
public static void main(String[] args){
int number = 5; //int datatype
System.out.print(number);
}
}
  • float : Any variable declared with this datatype can store decimal values that are 32 bit single precision floating point numbers….what does that mean?…well I honestly have no freaking idea lol !! I just googled about float and I got this…then why are you talking like as if you know everything? well…I never said I knew Android development…I am just learning android development and I am writing this blog as and when I learn new concepts on it, just read the intro blog whose link I have mentioned above. If you knew anything about single precision and double precision, you are free to explain them in the comments, it will be helpful for me too :) Jokes apart, for learning programming it is not necessary to learn about single point , double point precision and stuffs….I generally use float when I need to store a decimal number that has less than or equal to two digits after the decimal point, for more than two digits I tend to use another datatype which I will explain below.
public class datatypes{
public static void main(String[] args){
float number = 5.23; //float datatype
System.out.print(number);
}
}
  • Double : Any variable declared with this datatype can store decimal values that are double precision 64 bit floating point numbers….As I mentioned above I use this for values that have more than two digits after its decimal point.
public class datatypes{
public static void main(String[] args){
double number = 5.767676767676767; //double datatype
System.out.print(number);
}
}
  • Char : Any variable declared with this datatype can store characters like numbers, alphabets , symbols etc. and are defined inside double quotes. It returns a string value which means even if you store a number inside it, you can’t perform any arithmetic operations unless you convert those numbers to int or float or double. This conversion process is known as Typecasting which we will talk about later in the series.
public class datatypes{
public static void main(String[] args){
char text = "Hello"; //char datatype
System.out.print(text);
}
}
  • Boolean : Any variable declared with this datatype can store only either of the two values that is either true or false. This datatype is usually useful in verifying certain complex logic that you might use in your code and you are unsure if it works and also in many other places which you will understand later on as we progress.
public class datatypes{
public static void main(String[] args){
boolean check = true; //boolean datatype
int i = 7;
int j= 6;
if(i==j){
check=false; //In this if block we are checking if values
} //stored in i and j are equal if they are equal
//we are changing check value to false
System.out.print(check);
}
}

The commonly used Object datatype is…

  • String : Its actually a commonly used class for which we create an object and then we use those objects in our code….of course we will talk about class, objects etc. later on for now just know that there is something known as Object type.
public class datatypes{
public static void main(String[] args){
String text = "Hello"; //string object datatype. Here 'text' is not a variablee
System.out.print(text); //but an object of String class
}
}

These are the basic stuffs of variables and datatypes for getting you started. I hope it would be helpful to you…Alright I will see you guys next week, Goodbye !!

Link to the previous article in this series :

https://medium.com/@TechTiger/road-to-android-developer-season-1-java-intro-3034d3a7446a

Incase of any queries, reach out to me on,
Email : sktharanibalan@gmail.com
Instagram : name_is_tharani_

Disclaimer : The technical stuffs I am mentioning in this series might be factually wrong at some places or it might be short on content so I would recommend to read this stuff just as a refresher or for getting started as a beginner. For more detailed stuff follow other sites out there on the internet. Thank you

--

--

Tharani balan SK

programmer, App developer,Writer and Cybersecurity enthusiast