Java 0-10

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Java For

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 is a programming language which is used in Android App


Development. It is class based and object oriented programming whose
syntax is influenced by C++. The primary goals of JAVA is to be simple,
object-oriented, robust, secure and high level.

JAVA application runs on JVM (JAVA Virtual Machine) but Android has its
own virtual machine called Dalvik Virtual Machine (DVM) optimized for
mobile devices.

Prerequisites For JAVA:

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.

Other Tools: IntelliJ and Netbeans

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.

We Recommend Using Netbeans & IntelliJ


Variables
Variables is something that makes computer program very useful by storing
information in memory. When you think of variables think of boxes which
is used to store something. In computer world you can have hundreds,
thousands or more number of boxes (mean variables) each containing their
own pieces of information. That box is called variables in JAVA.

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:

Variable Example In JAVA

int x;
Here int is integer primitive data type and x is the variable.

In These Tutorials You Will Learn:


1 Syntax Of Variables:
2 Types Of Variables

Syntax Of Variables:

What Is Syntax: Syntax in programming language basically means


combinations of symbols based on the set of rules defined for that
language. JAVA has its own rules for syntax of variables and we need to
follow it.

Syntax of variables in JAVA is divided in 3 parts: first assigning type of data


you are storing (int,char,byte etc.), second is naming (lets say x) and third
is putting semicolon(;) which is telling program statement has end here.
Types Of Variables
A variables can be defined as to reserve memory space to store the values. These
can be initialized at the time of declaration or later on depending on the type of
variable. There are basically three type of variable available in java: local
variables, instance variables and static variables.

Table of Contents [hide]


1 Types Of Variables in JAVA:
2 Examples Of Variables In JAVA:
Types Of Variables in JAVA:

There are 3 types of variables in JAVA:

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.

Class Variable/Static Variable:


These are loaded and initialized when class is loaded in JVM
There exists only one copy of class variable
They live on heap memory. If these variables are not initialized to some default
value is assigned to them implicitly.
Data Types
When we say Data Type think of type of information you want to store in
variables like integer, character, decimal etc. Primitive means this data type
are pre-defined by JAVA language.

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.

Suppose we try to insert value 2,147,483,648 in integer variable:

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:

It is a 16-bit character using the Unicode encoding scheme has a minimum


value of \u0000 (or 0) and a maximum value of \uffff. Example of char data
type:

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:

char myChar = AB;


You will get an error too many character.

float:
It is a single-precision 32-bit (4-byte) floating-point value. Example for float data
type:

float myNumber = 5.25f;


Important Note: Put f after float number because it is good practice and by
default a decimal number in JAVA is assume as a double.

double:

It is a double precision 64-bit (8-byte) floating-point value. Example of double


data type:

double myNumber = 5.234d;


or
double myNumber = 5.234;
Both are accepted as by default decimal number is assume as double in JAVA.
But prefer putting d because it is considered as good practice in good coding.

boolean:

boolean data type has only two possible values: true and false. It is very useful
for conditional logic. Example of Boolean data type:

boolean isEven = true;

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.

String x = This is a String;


Here we use double quote to represent a string which is storing characters.
Strings
String is the most commonly used class in Java programming language. In java
every string we create is an object of String Class that implements Serializable,
Comparable and CharSequence interfaces. It is nothing but a character array for
example AbhiAndroid is a string of 11 characters as shown.

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.

Creating a String In JAVA:

Strings can be created very easily, by assigning a string value to the string literal
as shown:

String string1 = Welcome To Master Android App";


String string2 = " Welcome To Master Android App ";

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.

Using New keyword:

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

String string1 = new String(" Welcome To Master Android App ");


String string2 = new String(" Welcome To Master Android App ");
In this approach compiler will create two different objects in memory having
the same value.
Operators
In computer programming Operator is a symbol that tells
the compiler to perform specific action which can be
mathematical or logical. For example: + is a additive
operator which does addition of two number and can also
be used for String concatenation.

Lets take simple example to understand operator:


int x = 4 + 5 7;
In the above example:

+ and are addition and subtraction operator


respectively which comes under Arithmetic Operator.)
4, 5 and 7 are operand (Operand are objects that are
manipulated by operator).
= is an assignment operator which assign result of
calculation (value 2 in this case) to x.

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.

double kilometres = (50 * 1.609344);


In the above statement kilometres = (50 * 1.609344) is an expression and data
type double is not part of an expression. Typically except data type and ; forms
an expression.

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.

You might also like