0% found this document useful (0 votes)
9 views

1.2%20Fundamentals%20of%20Java%20Programming.pptx_0

fundamentals of java

Uploaded by

mitchellekoech31
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

1.2%20Fundamentals%20of%20Java%20Programming.pptx_0

fundamentals of java

Uploaded by

mitchellekoech31
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 30

Fundamentals of Java Programming

By Almodad Mutinda
|
Course Objectives
At the end of this lecture, each student should:
• Understand Java’s primitive types
• Understand variables in java.
• Understand Data Types in java.
• Understand Operators in java.

MOBILE APPLICATION DEVELOPMENT | 2


Variables in Java

• A variable is a name which is associated with a value that


can be changed.
• For example when I write int i=10;
- here variable name is i which is associated with value
10, int is a data type that represents that this variable can
hold integer values.

MOBILE APPLICATION DEVELOPMENT | 3


How to Declare a variable in Java

• To declare a variable follow this syntax:


data_type variable_name = value;
• here value is optional because in java, you can declare the
variable first and then later assign the value to it.
• For example: Here num is a variable and int is a data type.
We will discuss the data type in next tutorial so do not
worry too much about it, just understand that int data type
allows this num variable to hold integer values.
int num;
• Similarly we can assign the values to the variables while
declaring them, like this
char ch = 'A';
int number = 100;

MOBILE APPLICATION DEVELOPMENT | 4


Variables naming convention in java

• Variables naming cannot contain white spaces,


• for example: int num ber = 100; is invalid because
the variable name has space in it.
Variable name can begin with special characters such as $
and _
• As per the java coding standards the variable name should
begin with a lower case letter, for example int number; For
lengthy variables names that has more than one words do
it like this: int smallNumber; int bigNumber; (start the
second word with capital letter).
• Variable names are case sensitive in Java.

MOBILE APPLICATION DEVELOPMENT | 5


Types of Variables in Java

• There are three types of variables in Java.


1) Local variable
2) Static (or class) variable
3) Instance variable

MOBILE APPLICATION DEVELOPMENT | 6


Static (or class) Variable

• Static variables are also known as class variable because


they are associated with the class and common for all the
instances of class.
• For example, If I create three objects of a class and access
this static variable, it would be common for all, the changes
made to the variable using one of the object would reflect
when you access it through other objects.

MOBILE APPLICATION DEVELOPMENT | 7


Static (or class) Variable Example

MOBILE APPLICATION DEVELOPMENT | 8


Static (or class) Variable Output

MOBILE APPLICATION DEVELOPMENT | 9


Instance variable

• Each instance(objects) of class has its own copy of instance


variable.
• Unlike static variable, instance variables have their own
separate copy of instance variable.
• We have changed the instance variable value using object
obj2 in the following program and when we displayed the
variable using all three objects, only the obj2 value got
changed, others remain unchanged.
• This shows that they have their own copy of instance
variable.

MOBILE APPLICATION DEVELOPMENT | 10


Example of Instance variable

MOBILE APPLICATION DEVELOPMENT | 11


Static (or class) Variable Output

MOBILE APPLICATION DEVELOPMENT | 12


Local Variable

• These variables are declared inside method of the class.


• Their scope is limited to the method which means that You
can’t change their values and access them outside of the
method.
• In this example, I have declared the instance variable with
the same name as local variable, this is to demonstrate the
scope of local variables.

MOBILE APPLICATION DEVELOPMENT | 13


Example of Local variable

MOBILE APPLICATION DEVELOPMENT | 14


Static (or class) Variable Output

MOBILE APPLICATION DEVELOPMENT | 15


Data Types in Java

• Data type defines the values that a variable can take, for
example if a variable has int data type, it can only take
integer values.
• In java we have two categories of data type:
1) Primitive data types
2) Non-primitive data types – Arrays and Strings are
non- primitive data types,
• Java is a statically typed language.
• A language is statically typed, if the data type of a variable
is known at compile time. This means that you must specify
the type of the variable (Declare the variable) before you
can use it.

MOBILE APPLICATION DEVELOPMENT | 16


Primitive data types

• In Java, we have eight primitive data types: boolean, char,


byte, short, int, long, float and double.
• Java developers included these data types to maintain the
portability of java as the size of these primitive data types
do not change from one operating system to another.
• byte, short, int and long data types are used for storing
whole numbers.
• float and double are used for fractional numbers.
• char is used for storing characters(letters).
• boolean data type is used for variables that holds either
true or false.

MOBILE APPLICATION DEVELOPMENT | 17


Operators in Java

• An operator is a character that represents an action, for


example + is an arithmetic operator that represents
addition.

MOBILE APPLICATION DEVELOPMENT | 18


Types of Operators in Java

1. Basic Arithmetic Operators


2. Auto-increment and Auto-decrement Operators
3. Logical Operators
4. Comparison (relational) operators
5. Ternary Operator

MOBILE APPLICATION DEVELOPMENT | 19


1. Basic Arithmetic Operators

Purpose Operato Example Comments


r
Addition + sum = num1 + num2;
If num1 is 10 and
num2 is 2, sum is 12.
Subtraction – diff = num1 –
num2;
If num1 is 10 and
num2 is 2, diff is 8.

Multiplication * prod = num1 *


num2;
If num1 is 10 and
num2 is 2, prod is 20.

Division / quot = num1 / Division returns an


num2; integer value
If num1 is 31 and (with no
num2 is 6, quot is 5. remainder).

MOBILE APPLICATION DEVELOPMENT | 20


Basic Arithmetic Operators

Purpose Operato Example Comments


r
Remainder % mod = num1 % num2; Remainder finds
If num1 is 31 and the remainder of
num2 is 6, mod is 1. the first number
divided by the
second number.
5 R
6 31
30
-----
1
Remainder always
gives an answer
with the same
sign as the first
operand.

MOBILE APPLICATION DEVELOPMENT | 21


2. Comparison (relational) operators

MOBILE APPLICATION DEVELOPMENT | 22


3. The Logical Operators

MOBILE APPLICATION DEVELOPMENT | 23


4. The Assignment Operators

MOBILE APPLICATION DEVELOPMENT | 24


5. Ternary Operator

• This operator evaluates a boolean expression and assign


the value based on the result.

• If the expression results true then the first value before the
colon (:) is assigned to the variable num1 else the second
value is assigned to the num1.

MOBILE APPLICATION DEVELOPMENT | 25


Operator Precedence

Rules of precedence:
1.Operators within a pair of parentheses
2.Increment and decrement operators
3.Multiplication and division operators,
evaluated from left to right
4.Addition and subtraction operators,
evaluated from left to right

MOBILE APPLICATION DEVELOPMENT | 26


Type Casting

– Type casting lowers the range of a value, quite literally chopping it down
to a smaller size, by changing the type of the value (for example, by
converting a long value to an int value).
– You do this so that you can use methods that accept only certain types
as arguments, so that you can assign values to a variable of a smaller
data type, or so that you can save memory.
– Put the target_type (the type that the value is being type cast to) in
parentheses in front of the item that you are type casting. The syntax for
type casting a value is:
» identifier = (target_type) value
– where:
• identifier is the name you assign to the variable
• value is the value you want to assign to the identifier
• (target_type) is the type to which you want to type cast the value.
Notice that the target_type must be in parentheses.

MOBILE APPLICATION DEVELOPMENT | 27


Type Casting Example

• Syntax:
identifier = (target_type) value
• Example of potential issue:
int num1 = 53; // 32 bits of memory to hold the value
int num2 = 47; // 32 bits of memory to hold the value
byte num3; // 8 bits of memory reserved
num3 = (num1 + num2); // causes compiler error
• Example of potential solution:
int num1 = 53; // 32 bits of memory to hold the value
int num2 = 47; // 32 bits of memory to hold the value
byte num3; // 8 bits of memory reserved
num3 = (byte)(num1 + num2); // no data loss

MOBILE APPLICATION DEVELOPMENT | 28


Type Casting Example

Examples:
int myInt;
long myLong = 99L;
myInt = (int) (myLong); // No data loss, only zeroes.
// A much larger number would
// result in data loss.
int myInt;
long myLong = 123987654321L;
myInt = (int) (myLong); // Number is "chopped"

MOBILE APPLICATION DEVELOPMENT | 29


Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|

You might also like