Vous êtes sur la page 1sur 39

Introduction to Java

Objectives
In this lesson, you will learn about:

Variables and Literals Primitive Data Types Abstract Data Types Types of Variables Literals in Java Using Conditional Statements

Introduction to Java

Declaring Variables and Literals (Contd.)

Defining Variables and Literals: A variable is the name that refers to a memory location where some data value is stored. Each variable that is used in a program must be declared.

Introduction to Java

Declaring Variables and Literals (Contd.)

Naming Conventions for Variables: The name of a variable needs to be meaningful, short, and without any embedded space or symbol. A variable name must be unique. A variable name must begin with a letter, an underscore (_), or the dollar symbol ($), which can be followed by a sequence of letters or digits (0 to 9), $, or _ . A variable name should not start with a digit. A variable name should not contain embedded white spaces . A variable name should not consist of a keyword. A variable name in Java is case sensitive.

Introduction to Java

Declaring Variables and Literals

The various data types in Java are: Primitive or the simple data types Abstract or the derived data types

Introduction to Java

Declaring Variables and Literals (Contd.)


Group Data Type byte Size Range Default Value 0 Integer One byte -27 to 271 (signed) -215 to 215 1 -231 to 231-1 -263 to 263-1

short int long

Two byte Four byte Eight byte

0 0

Introduction to Java

Declaring Variables and Literals (Contd.)


Group Data Type float double Boolean boolean Size Range Default Value 0.0 0.0 false Floating point Four byte Eight byte One bit 3.4e-038 to 3.4e+038 1.7e-308 to 1.7e+308 true or false

Character

char

Two byte

a single character

null

Introduction to Java

Declaring Variables and Literals (Contd.)

byte short int Data type Width (Bits) Integer data types and their width and ranges 8 16 32 Range -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 -9,223,372,036,854, 775,808 to 9,223,372, 036, 854,775,807

long

64

Introduction to Java

Declaring Variables and Literals (Contd.)



Floating point data types: Float:Has a range of 32 bits Double:Has a range of 64 bits Character data type: Has a width of 16-bits Has the set of standard ASCII character ranging from 0 to 127

Introduction to Java

Declaring Variables and Literals (Contd.)

Abstract data types: Data types derived from the primitive data types String stores letters, digits, and characters such as as /, (), :, :, $, and #.

Introduction to Java

Declaring Variables and Literals (Contd.)

Keywords available in Java: abstract case const double finally if boolean catch continue else float implements break char default extends for Import byte class do final goto instanceof

Introduction to Java

Declaring Variables and Literals (Contd.)

Keywords available in Java: (Contd.) int new public strictfp this try interface package return super throw void long private short switch throws volatile native protected static synchronized transient while

Introduction to Java

Declaring Variables and Literals (Contd.)

Types of Variables: Class variables Instance variables Local variables Static variables

Introduction to Java

Declaring Variables and Literals (Contd.)



Class Variables

Are accessible within a class and its objects. The class variables are declared inside the class before their use. Are declared inside a class and are created when the class is instantiated. Objects give different values to instance variables as per the specific requirements of the object of that class type.

Instance Variables

Introduction to Java

Declaring Variables and Literals (Contd.)

Local Variables

Are declared inside a method. Their scope is within the block of code in which they are defined. They are local to the block of code and are not accessible outside the method. Are allocated memory only once but are globally accessible to all instances of a class. Therefore, when an instance of a class is destroyed, the static variable is not destroyed and is available to other instances of that class.

Static Variables

Introduction to Java

Declaring Variables and Literals (Contd.)

Literals in Java:

Literals are the values to be stored in variables and constants. A literal contains a sequence of characters, such as digits, alphabets, or any other symbol that represents the value to be stored. The various types of literals in Java are:

Integer literals Floating point literals Character literals String literals Boolean literals

Introduction to Java

Declaring Variables and Literals (Contd.)

Integer Literals

Are numeric type values. The numerical values can be represented in octal and hexadecimal notation. The octal notation of a number is prefixed by zero and hexadecimal numbers are prefixed by 0x. For example, n=0123 is integer literal in octal notation, n=0x456 is integer literal in hexadecimal notation, and n=2 is decimal notation for an integer literal.

Introduction to Java

Declaring Variables and Literals (Contd.)



Floating Point Literals

Are numeric values with fractional part. For example, x=7.9 is a floating point literal. Are represented in single quotation marks. For example, x='k' is a character literal.

Character Literals

Introduction to Java

Declaring Variables and Literals (Contd.)



String Literals

Are enclosed in double quotation marks. For example, x="James" is a string literal. Are the literals having value, true or false. For example, x= false is a Boolean literal.

Boolean Literals

Introduction to Java

Declaring Variables and Literals (Contd.)

Manipulating Variables: Assignment Operator You use the assignment operator (=) to assign a value to a variable.

Introduction to Java

Declaring Variables and Literals (Contd.)


Operator + * / Adds two operands Subtracts one operand from another Multiplies two operands Divides two operands Operation

Introduction to Java

Declaring Variables and Literals (Contd.)


Operator % ++ Operation Calculates the modulus Increments a variable

--

Decrements a variable

Introduction to Java

Using Conditional Statements



Conditional statements allow selective execution of a set of statements depending on the value of expressions associated with them. Conditional statements are also known as decision-making statements. You can control the flow of a program using conditional statements. Two types of conditional statements in Java are: The if-else statement The switch-case construct

Introduction to Java

Using Conditional Statements (Contd.)

Using the if-else Statement The if-else statement: Enables you to execute selectively. Is followed by a boolean expression. Executes the set of statements depending upon the result of the boolean expression.

Introduction to Java

Using Conditional Statements (Contd.)

Using the if-else Statement (Contd.) Syntax of the if-else statement is: if(boolean expression) { statement(s) } else { statement(s) }

In the preceding syntax, if the boolean expression of the if construct evaluates to true, the Java compiler executes the statements following the if construct else executes the statements following the else construct.

Introduction to Java

Using Conditional Statements (Contd.)

Relational Operators: Used to compare the values of two variables or operands and find the relationship between the two. The relational operators are therefore called comparison operators also.

Introduction to Java

Using Conditional Statements (Contd.)

The following table lists the various relational operators (Contd.): Operator > Use op1 > op2 Operation Returns true if the value of the op1 operand is greater than the value of the op2 operand. Returns true if the value of the op1 operand is less than the value of the op2 operand. Returns true if the value of the op1 operand is greater than or equal to value of op2 operand.

<

op1<op2

>=

op1>=op2

Introduction to Java

Using Conditional Statements (Contd.)


Operator <= Use op1<=op2 Operation Returns true if value of op1 operand is less than or equal to value of op2 operand. Returns true if value of op1 operand is equal to value of op2 operand. Returns true if value of op1 operand is not equal to value of op2 operand.

==

op1 ==op2

!=

op1 != op2

Introduction to Java

Using Conditional Statements (Contd.)

Conditional Operators : Used to combine multiple conditions in one boolean expression. Are of two types : Unary Binary

Various conditional operators are: AND(&&) OR(||) NOT(!)

Introduction to Java

Using Conditional Statements (Contd.)

The following table lists the various conditional operators and their operations: Operator Use Operation

AND (&&)

op1 && op2

Returns true if both op1 operand and op2 operand are true.
Returns true if either op1 operand or op2operand is true. Returns true if op1 operand is not true.

OR (||)

op1 || op2

NOT (!)

! op1

Introduction to Java

Using Conditional Statements (Contd.)

Using the multiple if-else Statement: You can replace a single if construct with multiple if-else statements to write a compound if statement.

The multiple if-else construct allows you to check multiple boolean expressions in a single compound if statement.

Introduction to Java

Using Conditional Statements (Contd.)

Using the multiple if-else Statement (Contd.): The syntax of the multiple if-else statements is: if (Boolean_expression_1) { statements } else if (Boolean_expression_2) { statements } else (Boolean_expression_3) { statements}

Introduction to Java

Using Conditional Statements (Contd.)

The switch-case Construct: Successively tests the value of an expression or a variable against a list of case labels with integer or character constants. When a match is found in one of the case labels, the statements associated with that case label get executed. The switch keyword in the switch-case construct contains the variable or the expression whose value is evaluated to select a case label. The case keyword is followed by a constant and a colon.

The data type of case constant should match the switch variable.

Introduction to Java

Using Conditional Statements (Contd.)

The switch-case Construct (Contd.) The syntax of the switch-case construct is: switch(expression or variable name) { case Expr1: statements; break; case Expr2: statements; break; default: statements; }

Introduction to Java

Using Conditional Statements (Contd.)

The switch-case construct (Contd.) Statements associated with the default keyword are executed if the value of the switch variable does not match any of the case constants. The break statement used in the case label causes the program flow to exit from the body of the switch-case construct.

Introduction to Java

Using Conditional Statements (Contd.)

The break Statement: Causes the program flow to exit from the body of the switch construct. Control goes to the first statement following the end of the switchcase construct. If not used inside a case construct, the control passes to the next case statement and the remaining statements in the switch-case construct are executed.

Introduction to Java

Demonstration-Scholarship Criteria of Certified Careers Institute

Problem Statement The Certified Careers institute is granting scholarship to those students who passed in each subject and scored more than 175 marks out of 200 marks. Steve is a Java developer in the institute. He is assigned the task for developing the Java application that enables the user to select the eligible students. Before creating the application for all the students he is testing the application for a specific student David. David has passed each subject and scored 180 marks. Help Steve to code the application for David.

Introduction to Java

Demonstration-Scholarship Criteria of Certified Careers Institute(Contd.)

Solution To solve the problem, Steve can use the if-else statement, conditional operators, and relational operators to code the selection procedure. A student is applicable for scholarship only if the student passes in each subject and total marks scored are more than 175 out of 200. To solve the given problem, perform the following tasks : 1. Code the application. 2. Compile and execute the application.

Introduction to Java

Summary
In this lesson, you learned: Java is both a language and a technology used to develop desktop and Internet-based applications known as Java applications and applets. The various characteristics of Java programming language Simple Object-oriented Compiled and interpreted Portable Distributed Robust Secure Various components of the Java architecture are: Java programming language Java class file Java Virtual Machine Java Application Programming Interface (API).

Introduction to Java

Summary(Contd.)

The JVM converts the bytecode contained in the .class file to machine object code. The various data types defined in Java are: Integers: Include byte, short, int, and long data types. Floating-point numbers: Include double and float data types. Characters: Include char, data type. Boolean: Include boolean, data type. The built-in or the intrinsic data types in Java are known as the primitive or the simple data types. The abstract data types include the data types derived from the primitive data types. The keywords are the reserved words for a language, which express the language features.