Java 0-10
Java 0-10
Java 0-10
Android
Android Apps are mostly developed in JAVA language using Android
SDK (Software Development Kit). Other languages like C, C++, Scala etc. can
also be used for developing Android App, but JAVA is most preferred and
mostly used programming language for Android App Development. So if you
are a beginner in Android then JAVA language and complete knowledge of
OOPS concepts is the first thing you need to learn before beginning Android
Development.
Introduction To JAVA
JAVA application runs on JVM (JAVA Virtual Machine) but Android has its
own virtual machine called Dalvik Virtual Machine (DVM) optimized for
mobile devices.
Eclipse:
Being a JAVA programmer you will need some tools or software to code and
run it. Lots of tools are available over the web but we recommend you to
use Eclipse for learning JAVA since it is the most common tool used for
Android Development along with Android Studio. So getting habitual with
Eclipse and its shortcuts will be bonus in your Android journey. Other then
Eclipse you can also prefer IntelliJ or netbeans for learning JAVA.
Android Studio: Are you thinking of using Android Studio for learning JAVA?
Then Android Studio is solely meant for Android programming. IntelliJ,
Eclipse and Netbeans are for real Java codings.
While creating variables in JAVA we need to assign two things. First is the
type of information we want to store and second is the name of the
variable. Below is an example of variable where int is a type of information
(integer in this case) and x is the name:
int x;
Here int is integer primitive data type and x is the variable.
Syntax Of Variables:
1. Local Variable
2. Instance variable
3. Class Variable/Static variable
Local variable:
These are also called as stack variable. Because they exist in stack memory
It is mandatory to initialize the local variable. Otherwise you will get run time
error from compiler
These can be defined inside method, constructor or also inside block. The scope
or life time of local variable destroyed with end of method completion.
Instance variable:
Instance variable are also known as member variable or field
These are associated with the object creation. As the object get created instance
variable also get created
These live on heap memory. In case, if you dont initialize instance variable with
initial value these get default value at run time implicitly.
JAVA has 8 reserved keyword for primitive data type for assigning 8 different
type of information based on value (type of information) and byte (memory
or space). For example int is used for a 32-bit (4-byte) integer value, char for
16-bit character, boolean for true or false value, short for 16-bit (2-byte)
integer value etc.
In JAVA this data type must be declared with variables when creating them.
This helps compiler to ensure we are inserting the right type of data in
variables which we have assigned with a particular data type.
Lets try to understand the concept with examples of int data type which is
used for assigning integer (number without a decimal point).
Suppose we want to store initial value 5 in x variables:
int x = 5;
But what if we want to store 5.2 in integer data type:
int x = 5.2;
The compiler will throw error because we are trying to store decimal value in
integer data type.
Important Note: One more important thing to note is that data type also
assign the limit of space that can be store in variables. In computing terms
there is always a limit of value minimum and maximum that can be stored.
For example int can store minimum value of -231 and a maximum value of
231-1. If you try to store higher value than 231-1 or lesser value than -231
then compiler will throw an error.
int x = 2,147,483,648;
The compiler will give an error because we are trying to store higher value
than 231-1. If you want to store that particular value then you have to use
long data type instead of int. This concept will make more sense as you
proceed in this article where we will discuss 8 different Primitive data types
in JAVA.
Below are the 8 different primitive data types:
byte:
It is a 8-bit (1-byte) integer value which has a minimum value of -128 and a
maximum value of 127. Byte examples:
byte x = 120;
Suppose if we try to store -200 in byte data type:
byte x = -200;
Compiler will throw an error for exceeding the limits of memory allocated for
byte type.
short:
It is a 16-bit (2-byte) integer value which has a minimum value of -32,768 and a
maximum value of 32,767. Example for short data type:
short x = 30000;
int:
It is a 32-bit (4-byte) integer value which has a minimum value of -231 and a
maximum value of 231-1. int is the most common, preferred and widely used
data type in Android. Example for int data type:
int x = 1245678;
long:
It is a 64-bit (8-byte) integer value which has a minimum value of -263 and a
maximum value of 263-1. This data type is also preferred in Android for storing
very large value like game high score which can be in billions. Example for long
data type:
long x = 12345987609L;
Remember to use either l or L in long data type after the value because it
tells the computer we are storing value in long data type.
char:
char myChar = 0;
In char we use single quote for representing character.
Important Note: In char data type we can only store one character, number,
special character or Unicode character. To store more than one character we use
String. We will discuss basics of string in this article after finishing eight primitive
data type.
Lets try to put more than one character in char data type:
float:
It is a single-precision 32-bit (4-byte) floating-point value. Example for float data
type:
double:
boolean:
boolean data type has only two possible values: true and false. It is very useful
for conditional logic. Example of Boolean data type:
String:
String is a sequence of characters. As we saw previously char is limited to store
just one character or Unicode character while String can store sequence of
characters.
char[] test= { 'A' , 'b' , 'h' , 'i' , 'A' , 'n' , 'd' , 'r' , 'o' , 'i' , 'd' };
String testString= new String(test);
Strings are Immutable in nature, which means once string is created its value
cannot be altered, but we can create a new Instance of it.
Immutable: Any object whose state cannot be altered once created are called
Immutable objects. String, Integer, Byte, Short, all other wrapper class objects are
immutable in nature.
Strings can be created very easily, by assigning a string value to the string literal
as shown:
As, discussed above String is a class, but we have not created any object above
using new keyword, so how new object is created? Dont worry compiler did
your task. But problem here is that, if the object is already present in the
memory compiler do not create a new object rather it assigns the same old
object to the new instance created, which means even though we have two
string instances above string1 and string2, compiler only creates one string
object having the value Welcome To Master Android App and assigns the
same to both the instances String1 and String2.
As we saw above, by using only String literal compiler assigned the same string
object to two different string literals, To overcome this approach we can create
string using new keyword as shown
Operator Precedence
Operator Precedence
postfix expr++, expr
unary ++expr, expr, +expr, -expr, ~, !
multiplicative *, /, %
additive +,
shift <<, >>, >>>
relational <, >, <=, >=, instanceof
equality ==, !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?:
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=,
assignment >>>=
Reserved Words in
Java
Expression, Statement And Code Blocks
Expressions:
Expressions are building blocks off all JAVA programs which are made up of
variables, operators, and method invocations. The expression are created
according to the syntax and evaluates to a single value. For example, if we want
to covert miles to kilometre we first need to know a mile is equal to
1.609344km. Now lets suppose we want to convert 50 miles to kilometres.
Statement:
Statement forms a complete unit of execution. All statements are terminated
with semicolon (;)
int myRoll = 3;
The above complete line is a statement. In JAVA following types form
expressions of assignment: any use of ++ or , method invocations and
object creation expressions can be made into statement by terminating with
semicolon(;);
Blocks:
A blocks in JAVA is a group of zero or more statements enclosed between
braces. The block begins with opening braces ({) and ends with closing braces
(}). Even though it is a statement but doesnt end with semicolon(;). For
example:
{
int myRoll = 3;
String myName = Abhishek;
}
It is mostly used with control flow statement. For example:
If (x == 50){
System.out.println(Example of control flow statement);
x--;
}
In the above example all code inside braces {} is part of blocks.