An overview of Java, Data types and variables

Similar documents
BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Program Fundamentals

Java Programming. Atul Prakash

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3. Java - Language Constructs I

2 rd class Department of Programming. OOP with Java Programming

COMP 202 Java in one week

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

CSC 1214: Object-Oriented Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Language Fundamentals Summary

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

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

Building Java Programs. Introduction to Programming and Simple Java Programs

Getting started with Java

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CompSci 125 Lecture 02

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Java Programming Language Mr.Rungrote Phonkam

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

The MaSH Programming Language At the Statements Level

Java Bytecode (binary file)

Programming Language Concepts: Lecture 2

The Java Language The Java Language Reference (2 nd ed.) is the defining document for the Java language. Most beginning programming students expect

ECE 122 Engineering Problem Solving with Java

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Imports. Lexicon. Java/Lespérance 1. PROF. Y. LESPÉRANCE Dept. of Electrical Engineering & Computer Science

Getting started with Java

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Pace University. Fundamental Concepts of CS121 1

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programming in C++ 4. The lexical basis of C++

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Java Overview An introduction to the Java Programming Language

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

DM550 Introduction to Programming part 2. Jan Baumbach.

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

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

Programming. Syntax and Semantics

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

Strings, Strings and characters, String class methods. JAVA Standard Edition

Accelerating Information Technology Innovation

CS 177 Recitation. Week 1 Intro to Java

6.096 Introduction to C++ January (IAP) 2009

Section 2.2 Your First Program in Java: Printing a Line of Text

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

CS 11 java track: lecture 1

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

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Java Basic Programming Constructs

Introduction to Programming (Java) 2/12

PROGRAMMING FUNDAMENTALS

4 Programming Fundamentals. Introduction to Programming 1 1

1 Shyam sir JAVA Notes

Tony Valderrama, SIPB IAP 2009

3. Java - Language Constructs I

Programming Language Concepts: Lecture 2

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

STRUCTURING OF PROGRAM

Preview from Notesale.co.uk Page 9 of 108

CHAPTER 2 Java Fundamentals

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Introduction to C# Applications

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

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Section 2.2 Your First Program in Java: Printing a Line of Text

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Introduction to Java

Data Types and Variables in C language

Chapter 2: Using Data

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Basic Elements of C. Staff Incharge: S.Sasirekha

Building Java Programs. Chapter 1: Introduction to Java Programming

A variable is a name that represents a value. For

MP 3 A Lexer for MiniJava

CSC Java Programming, Fall Java Data Types and Control Constructs

The Java Language Rules And Tools 3

Java Basic Datatypees

Procedures, Parameters, Values and Variables. Steven R. Bagley

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Language Reference Manual

Basics of Java Programming

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

Full file at

Visual C# Instructor s Manual Table of Contents

Programming Lecture 3

Transcription:

An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1

2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(string args[]) { System.out.println( Hello World ); } }

3 Hello World: Java and C S1: S2: S3: S4: S6: // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(string args[]) { System.out.println( Hello World ); } } /* helloworld.c: Hello World program */ #define <stdio.h> void main(int argc, char *argv[]) { printf( Hello World\n ); }

4 Program Processing Compilation # javac HelloWorld.java results in HelloWorld.class Execution # java HelloWorld Hello World

5 Closer Look at - Hello World The class has one method main() public static void main(string args[]) { System.out.println( Hello World ); } Command line input arguments are passed in the String array args[] e.g java HelloWorld John Jane args[0] John args[1] Jane

6 Closer Look at - Hello World import java.lang.*; Java allows grouping of related classes into a package. It allows different companies can develop different packages, may even have same class and method names, but they differ by package name: ibm.mathlib.* microsoft.mathlib.* Helps in managing name clash problems. Think of this package as library. import statement somewhat serves similar purpose as C s #include If you don t add import statement, then you need utilise fully qualified name. ibm.mathlib.sin() If you do import ibm.* then you can use mathlib.sin() instead.

7 Java imports java.lang.* by default So, You don't need to import java.lang.* That means, you can invoke services of java s lang package classes/entities, you don t need to use fully qualified names. We used System.out.println() instead of java.lang. System.out.println()

8 public static void main(string args[]) public: The keyword public is an access specifier that declares the main method as unprotected. static: It says this method belongs to the entire class and NOT a part of any objects of class. The main must always be declared static since the interpreter users this before any objects are created. void: The type modifier that states that main does not return any value.

9 System.out.println( Hello World ); java.lang.* All classes/items in lang package of java package. System is really the java.lang.system class. This class has a public static field called out which is an instance of the java.io.printstream class. So when we write System.out.println(), we are really invoking the println() method of the out field of the java.lang.system class.

10 Java Program Structure Documentation Section Package Statement Import Statements Interface Statements Class Declarations Main Method Class { }

11 Basic Data Types Types boolean either true or false char 16 bit Unicode 1.1 byte 8-bit integer (signed) short 16-bit integer (signed) int 32-bit integer (signed) long 64-bit integer (singed) float 32-bit floating point (IEEE 754-1985) double 64-bit floating point (IEEE 754-1985) String (class for manipulating strings) Java uses Unicode to represent characters internally

Elements of Java lexical values, chapter 2 (pages 37-39) White spaces: Java is a free-form language. There is no special rue for us to follow. Identifiers: Identifiers are used for class names, method names and variables names. They must not begin with a number. The following is correct: height, a2, $sign The following is incorrect: 2dco, yes/no Literals: A constant value is created by using literal such as 200 (integer), 12.3 (floating-point value), DCO (string) 12

Elements of Java lexical values Comments: // (single line) /* */ (block) /**.. */ (documentation for HTML format Separators: separators are used to terminate statements () parentheses {} braces [] brackets ; semicolon, comma. Period 13

Java Keywords there are 49 words abstract finally public assert 3 float return boolean for short break goto 1 static byte if strictfp 2 case implements super catch import switch char instanceof synchronized class int this const 1 interface throw continue long throws default native transient do new try double package void else private volatile extends protected while 14 2017/2/13 final

Data Types and Variables Integers Floating point Characters Booleans Variables Type conversion (integer to floating) 15

Integers 4 types Name Size (bits) bytes Range byte 8 1-128 to 127 short 16 2-32768 to 32767 int 32 4-2147483648 to 2147483647 long 64 8-922372036854775808 to 9223372036854775807 16

Example of byte, short, int and long 17

Example out of range, (byte) 18

Floating 2 types Type size (bits) range Double 64 4.9exp(-324) to 1.8 exp(308) float 32 1.4exp(-45) to 3.4 exp(38) 19

Example long 20

Example out of range 21

Characters Java uses two bytes to store characters (0 to 65536) The standard of ASCII ranges from 0 to 127 For example, X in ASCII is 88 22

ASCII Table in hexadecimal (16) 23

Example character 24

Character.isLetter(ch) return true or false 25

Character.isDigit(ch) return true or false 26

Booleans Java has a simple type called boolean. It has two values, true or false For example, a = 4, b = 5, (a >b) is true For example, (4 = 4) is true For example, (3 > 4) is false 27

Example of boolean 28

Escape sequence Escape sequence uses \u to stand for white space. They are 16-bit values. Sequence name value \n newline \u000a \t tab \u0009 \b backspace \u0008 \r carriage return \u000d \f form feed \u000c \ single quote 29

String Strings literals are similar to English. Examples are: hello how are you? three \blines \ This statement is quoted \ 30

Example of String and Escape Sequence 31

Variables In java, variables are basic storage. The basic format is type identifier [= value], identifier [= value]; Examples are: int a =10, b = 20, c=30; int a, b, c; double a = 2.3, b = 4.5, d= 6.7; char x = z ; 32

Type conversion Byte defines the value between - 127 to 128. If you assign a value that is larger than 128, you have to use short or integer. This needs conversion. 33

Example 34

Example 35

Summary Integers 4 types, byte, short, int and long Floating point 2 types, float and double Characters ASCII, X Booleans true or false String How are you? Variables u67 (correct), 67u (incorrect) Type conversion (integer to floating) 36