Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Similar documents
Java Overview An introduction to the Java Programming Language

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Lexical Considerations

1 Lexical Considerations

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Expressions & Flow Control

Accelerating Information Technology Innovation

Course Outline. Introduction to java

Programming. Syntax and Semantics

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Lexical Considerations

4 Programming Fundamentals. Introduction to Programming 1 1

Computer Components. Software{ User Programs. Operating System. Hardware

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Language Fundamentals Summary

Full file at

BASIC ELEMENTS OF A COMPUTER PROGRAM

More Programming Constructs -- Introduction

JAVA Programming Fundamentals

Programming with Java

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

2 rd class Department of Programming. OOP with Java Programming

Zheng-Liang Lu Java Programming 45 / 79

Java Notes. 10th ICSE. Saravanan Ganesh

CGS 3066: Spring 2015 JavaScript Reference

PROGRAMMING FUNDAMENTALS

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CMPT 125: Lecture 3 Data and Expressions

Introduction to Programming Using Java (98-388)

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 3: Operators, Expressions and Type Conversion

3. Java - Language Constructs I

Operators and Expressions

Index COPYRIGHTED MATERIAL

B.V. Patel Institute of BMC & IT, UTU 2014

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS201 Some Important Definitions

Java Basic Datatypees

Basics of Java Programming

Program Fundamentals

Getting started with Java

Language Reference Manual simplicity

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Lecture 3. More About C

A Fast Review of C Essentials Part I

CS313D: ADVANCED PROGRAMMING LANGUAGE

Variables and literals

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Computer Components. Software{ User Programs. Operating System. Hardware

The Warhol Language Reference Manual

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Creating a C++ Program

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

UNIT- 3 Introduction to C++

Lecture Set 2: Starting Java

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Lecture Set 2: Starting Java

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Visual C# Instructor s Manual Table of Contents

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CS 231 Data Structures and Algorithms, Fall 2016

Brief Summary of Java

Pace University. Fundamental Concepts of CS121 1

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Java Bytecode (binary file)

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Reserved Words and Identifiers

Java Programming Fundamentals. Visit for more.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming in C++ 5. Integral data types

Java Basic Programming Constructs

Full file at

CEN 414 Java Programming

egrapher Language Reference Manual

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Sri Vidya College of Engineering & Technology

Computer System and programming in C

CHAD Language Reference Manual

Java is an objet-oriented programming language providing features that support

Introduction to Java & Fundamental Data Types

Lexical Structure (Chapter 3, JLS)

Transcription:

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Expressions, Statements and Arrays.

Java technology is: A programming language A development environment An application environment A deployment environment It is similar in syntax to C++. It is used for developing both applets and applications Introduction to Java Technology

Primary Goals of the Java Technology Provides an easy-to-use language by: Avoiding many pitfalls of other languages Being object-oriented Enabling users to create streamlined and clear code Provides an interpreted environment for: Improved speed of development Code portability

Primary Goals of the Java Technology Loads classes dynamically; that is, at the time they are actually needed Supports changing programs dynamically during runtime by loading classes from disparate sources Furnishes better security

Primary Goals of the Java Technology The following features fulfill these goals: The Java Virtual Machine Garbage collection The Java Runtime Environment (JRE)

Simple Object-Oriented Platform independent Secured Robust Architecture neutral Portable Dynamic Interpreted High Performance Multithreaded Distributed Feature of Java

Provides hardware platform specifications Reads compiled byte codes that are platform-independent Is implemented in a Java technology development tool or a Web browser The Java Virtual Machine

Allocated memory that is no longer needed should be deallocated. In other languages, deallocation is the programmer s responsibility. The Java programming language provides a system-level thread to track memory allocation. Garbage collection

Garbage collection has the following characteristics: Checks for and frees memory no longer needed Is done automatically Can vary dramatically across JVM implementations Garbage collection

The Java application environment performs as follows: The Java Runtime Environment

The JVM performs three main tasks: Loads code Verifies code Executes code JVM Tasks

Loads all classes necessary for the execution of a program Maintains classes of the local file system in separate namespaces The Class Loader

Ensures that: The code adheres to the JVM specification. The code does not violate system integrity. The code causes no operand stack overflows. The parameter types for all operational code are correct. No illegal data conversions (the conversion of integers to pointers) have occurred. The Bytecode Verifier

They are in-built part of the language. These data type have the same size and characteristics no matter what operating system and platform you are on. Primitive Data Type

Primitive Data Types Primitive Data Types Logical boolean Textual char Integral byte, short, int, and long Floating double and float

Logical The boolean primitive has the following characteristics: The boolean data type has two literals, true and false. For example, the statement: boolean truth = true; declares the variable truth as boolean type and assigns it a value of true.

The textual char primitive has the following characteristics: Represents a 16-bit Unicode character Must have its literal enclosed in single quotes ( ) Uses the following notations: 'a' The letter a '\t' The tab character '\u????' A specific Unicode character,????, is replaced with exactly four hexadecimal digits. For example, \u03a6 is the Greek letter phi [Φ]. Textual char

The textual String type has the following characteristics: Is not a primitive data type; it is a class Has its literal enclosed in double quotes (" ") "The quick brown fox jumps over the lazy dog." Can be used as follows: String greeting = "Good Morning!! \n"; String errormessage = "Record Not Found!"; Textual - String

The integral primitives have the following characteristics: Integral primates use three forms: Decimal, octal, or hexadecimal 2 : in Decimal 02: in Octal 0XB: in Hexadecimal Literals have a default type of int. Literals with the suffix L or l are of type long. Integral byte, short, int, and long

Byte: 8 bits Short: 16 bits int: 32 bits long: 64 bits Integral:

The floating point primitives have the following characteristics: Floating-point literal includes either a decimal point or one of the following: E or e (add exponential value) F or f (float) D or d (double) 3.14: A simple floating-point value (a double) 6.02E23: A large floating-point value 2.718F: A simple float size value Floating double and float

Literals have a default type of double. Floating-point data types have the following sizes: Float Length Name or Type 32 bits: float 64 bits: double Floating double and float

In Java technology, beyond primitive types all others are reference types. A reference variable contains a handle to an object. Java Reference Types

In addtion to the 8 basic data types, a variable can have a class as its type, String myname= abhilasha ; Color Hair; Class Type

Identifiers have the following characteristics: Are names given to a variable, class, or method Can start with a Unicode letter, underscore (_), or dollar sign ($) Are case-sensitive and have no maximum length Examples: identifier username user_name _sys_var1 $change Identifiers

A variable is a place where information can be stored while a program is running. The value can be changed at any point in the program. There are three types of variables in Java Instance Variable Class Variable Local Variable Variable

Instance variable: These are used to define an object s attribute. Class Variable: These are used to define the property of class Local Variables: these are used inside method definition or even smaller blocks of statements with in a method. Variable

Unlike other programming languages Java doesn t support global variable, a variable can be used in all parts of progarm. Global Variable

The three permissible styles of comment in a Java technology program are: First Type: // comment on one line Second Type: /* comment on one * or more lines */ Third Type: /** documentation comment * can also span one or more lines */ Comments

These are special symbols used for mathematical functions, some types of assignment statements, and logical comparisons. Operators

Addition: + Subtraction: - Multiplication: * Division: / Modulus: % Arithmetic

+= -= *= /= = Assignment

These are called prefix operators if listed before a variable name, and postfix operators if listed after a name. Incrementing and Decrementing

Equal: == Not Equal:!= Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >= Comparison Operators

and: && or: Logical Operators

Expressions perform operations on data and move data around. Some expressions will be evaluated for their results, some for their side effects, some for both. An expression can have three kinds of result: a value, such as the result of: (4 * i) a variable, such as the result of: i = 4 nothing (in the case of an invocation of a method declared as void) Expressions

An expression that results in a variable is called an lvalue in C++ and many other languages. A variable expression in Java is the same thing, the Java Language Specification just uses the name variable instead of lvalue. Such an expression can be used on the left hand side of an assignment operator. Side effects come about when an expression includes an assignment, increment, decrement, or method invocation. Expressions

A Java method body is a series of zero or more statements. In the Java programming language, statements are the fundamental unit of execution. All statements except blocks are terminated by a semicolon. Statements

Statements are executed for their effects; they do not have values. There are many different kinds of statements in Java: local variable declaration statements the if and if-else statements the while statement the do-while statement the for statement the switch statement the break statement the continue statement Statements

int i; String A= BCA 6 SEM ; double d=3.0; boolean b= true ; Variable declaration Statement

The if statement syntax: if ( <boolean_expression> ) <statement_or_block> Example: if ( x < 10 ) System.out.println( x less than 10?"); or (recommended): if ( x < 10 ) { System.out.println( x less than 10?"); } Simple if, else Statements

The if-else statement syntax: if ( <boolean_expression> ) <statement_or_block> else <statement_or_block> Example: if ( x < 10 ) { System.out.println("Are you finished yet?"); } else { System.out.println("Keep working..."); } Complex if, else Statements

The if-else-if statement syntax: if ( <boolean_expression> ) <statement_or_block> else if ( <boolean_expression> ) <statement_or_block> Example: int count = getcount(); // a method defined in the class if (count < 0) { System.out.println("Error: count value is negative."); } else if (count > getmaxcount()) { System.out.println("Error: count value is too big."); } else { System.out.println("There will be " + count + " people for lunch today."); } Complex if, else Statements

The switch statement syntax: switch ( <expression> ) { case <constant1>: <statement_or_block>* [break;] case <constant2>: <statement_or_block>* [break;] default: <statement_or_block>* [break;] } Switch Statements

The for loop: for (<init_expr>;<test_expr>; <alter_expr> ) <statement_or_block> Example: for ( int i = 0; i < 10; i++ ) System.out.println(i + " squared is " + (i*i)); or (recommended): for ( int i = 0; i < 10; i++ ) { System.out.println(i + " squared is " + (i*i)); } Looping Statements

The while loop: while ( <test_expr> ) <statement_or_block> Example: int i = 0; while ( i < 10 ) { System.out.println(i + " squared is " + (i*i)); i++; } Looping Statements

The do/while loop: do <statement_or_block> while ( <test_expr> ); Example: int i = 0; do { System.out.println(i + " squared is " + (i*i)); i++; } while ( i < 10 ); Looping Statements

The break [<label>]; command The continue [<label>]; command Special Loop Flow Control

break quits the loop without executing the rest of the statements in the loop. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration. Special Loop Flow Control

do { statement; if ( condition ) { break; } statement; } while ( test_expr ); The break Statement

Example

Output

Example

Output

do { statement; if ( condition ) { continue; } statement; } while ( test_expr ); Continue

Example

Output

Arrays are used typically to group objects of the same type. Arrays enable you to refer to the group of objects by a common name. The declaration of an array creates a reference that you can use to refer to an array. The actual memory used by the array elements is allocated dynamically either by a new statement or by an array initializer. Array

Declare arrays of primitive or reference types: char s[]; Point p[]; char[] s; Point[] p; Declare Array

Use the new keyword to create an array object. For example, a primitive (char) array: public char[] createarray() { char[] p; char s[]=new char[26]; p = new char[26]; for ( int i=0; i<26; i++ ) { s[i] =i; } return s; } Initializing Array

String names={ name1, name2, name3 }; This code is equivalent to: names=new String[3]; names[0]= name1 ; names[1]= name2 ; names[2]= name3 ; Examples:

Java language does not provide multidimensional array in the same way that other language do. In this you can create arrays of arrays. Example: int[][] twodim = new int[4][]; twodim[0] = new int[5]; twodim[1] = new int[50]; int[][] twodim = new int[][4]; //illegal Multidimensional Array

The object that is created by the first call to new is an array that contains four elements. Each element is a null reference to an element of type array of int and you must initialize each element separately so that each element points to its array. Multidimensional Array

Because of this separation, you can create non-rectangular arrays of arrays. Example: twodim[0] = new int[2]; twodim[1] = new int[4]; twodim[2] = new int[6]; twodim[3] = new int[8]; Array of four arrays of five integers each: int[][] twodim = new int[4][5]; Multidimensional Array

All array subscripts begin at 0: public void printelements(int[] list) { for (int i = 0; i < list.length; i++) { System.out.println(list[i]); } } Array bound

Java 2 Platform, Standard Edition (J2SE) version 5.0 introduced an enhanced for loop for iterating over arrays: public void printelements(int[] list) { for ( int element : list ) { System.out.println(element); } } The for loop can be read as for each element in list do. Using the Enhanced for Loop

You cannot resize an array. You can use the same reference variable to refer to an entirely new array, such as: int[] myarray = new int[6]; myarray = new int[10]; Array Resizing