Java Basic Programming Constructs

Similar documents
Operators in java Operator operands.

JAVA OPERATORS GENERAL

CSC 1214: Object-Oriented Programming

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Prof. Navrati Saxena TA: Rochak Sachan

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

SECTION II: LANGUAGE BASICS

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

The Arithmetic Operators

Operators. Java operators are classified into three categories:

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

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Program Fundamentals

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

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

Following is the general form of a typical decision making structure found in most of the programming languages:

Object Oriented Software Design

Chapter 3: Operators, Expressions and Type Conversion

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

An overview of Java, Data types and variables

Object-Oriented Programming

The Basic Parts of Java

Variables and literals

A flow chart is a graphical or symbolic representation of a process.

CT 229. Java Syntax 26/09/2006 CT229

CSC Java Programming, Fall Java Data Types and Control Constructs

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Expressions & Flow Control

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

The Java language has a wide variety of modifiers, including the following:

CS 106 Introduction to Computer Science I

DATA TYPES AND EXPRESSIONS

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Introduction to Java

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

Operators and Expressions

PROGRAMMING FUNDAMENTALS

1007 Imperative Programming Part II

Informatics Ingeniería en Electrónica y Automática Industrial

Java Primer 1: Types, Classes and Operators

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Learning the Java Language. 2.1 Object-Oriented Programming

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Quick Reference Guide

Building Java Programs

MODULE 02: BASIC COMPUTATION IN JAVA

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

13 th Windsor Regional Secondary School Computer Programming Competition

Zheng-Liang Lu Java Programming 45 / 79

Building Java Programs

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

4 Programming Fundamentals. Introduction to Programming 1 1

Getting started with Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS 11 java track: lecture 1

CIS133J. Working with Numbers in Java

Flow Control. CSC215 Lecture

Full file at

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Getting started with Java

Oct Decision Structures cont d

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

JAVA Programming Fundamentals

CS111: PROGRAMMING LANGUAGE II

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

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

Java+- Language Reference Manual

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Operators Questions

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Building Java Programs

Introduction to C Language

COMP6700/2140 Operators, Expressions, Statements

Preview from Notesale.co.uk Page 9 of 108

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

Eng. Mohammed S. Abdualal

Selected Questions from by Nageshwara Rao

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

Simple Java Reference

Java Notes. 10th ICSE. Saravanan Ganesh

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

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Course Outline. Introduction to java

4 WORKING WITH DATA TYPES AND OPERATIONS

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Transcription:

Java Basic Programming Constructs

/* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

This is a comment! /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

This is a class named HelloWorld. /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

The main function of your program. /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

The arguments of the program. /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

Output statement of your program. /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

/* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2

Programming Constructs 3

Programming Constructs input/output function object operator 2 Categories of Programming Constructs goto if then else while array Data Types break variable expression Control Flow pointer 3

/* * This is your second java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Example 4

/* * This is your second java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

A variable is defined as integer. Its name is i. /* * This It is is initialized your second to java 20. program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

This is a string literal. It contains The i is : /* * This Its is length your second is 11. java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

+ concatenates strings in Java. It can also concatenate integer with a string. The result is a string. /* * This is your second java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

The print functions writes without newline. The println functions writes with newline. /* * This is your second java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

/* * This is your second java program. */ class AnotherExample{ public static void main(string[] args){ int i = 20; System.out.println("The i is : " + i); i = i * i; System.out.print("The i ^ 2 is : "); System.out.println(i); Another Closer Look! 5

/* * This is your third java program. */ class YetAnotherExample{ public static void main(string[] args){ int i = 20; if ( i<30 ) System.out.println(" i < 30 ); else System.out.println( i > 30 ); Yet Another Example 6

If statement! /* * This Else is your statement! third java program. */ class YetAnotherExample{ public static void main(string[] args){ int i = 20; if ( i<30 ) System.out.println(" i < 30 ); else System.out.println( i > 30 ); Yet Another Example 6

A boolean expression. /* * This It is is your true third now. java program. */ class YetAnotherExample{ public static void main(string[] args){ int i = 20; if ( i<30 ) System.out.println(" i < 30 ); else System.out.println( i > 30 ); Yet Another Example 6

/* * This is your third java program. */ class YetAnotherExample{ public static void main(string[] args){ int i = 20; if ( i<30 ) System.out.println(" i < 30 ); else System.out.println( i > 30 ); Yet Another Example 6

Java Programming Constructs

Blocks {... if(x < 30){ y = 20; x = 30 * 30; 8

Blocks Blocks are defined with braces like C. Why blocks are used? Sometimes a group of statements needed to be executed in all or nothing manner. A block is the same as a single statement. {... if(x < 30){ y = 20; x = 30 * 30; 8

Variables int i; double i, j; long i = 10, j; 9

Variables 2 types of variable Class variable (Fields) Local variables There is no global variable. Variables can be defined in any place inside a class. Variables can be defined and initialized. Java uses static bindings. A variable always refers to its nearest enclosing binding. int i; double i, j; long i = 10, j; 9

Operators i = i - j; i = i * i; s = aaa + i; i++; i -= 1; 10

Operators An operator in Java is the same as operators in C++, but they cannot be overrode. =, +, -, ==, =, -=,... i = i - j; i = i * i; s = aaa + i; i++; i -= 1; 10

Constants 1, 1d, 1l, 1.1, 0xFF, 077 c, A, Java, ca true, false null 11

Constants Numerical constants Characters String literals Booleans nil 1, 1d, 1l, 1.1, 0xFF, 077 c, A, Java, ca true, false null 11

Expression j == i,!i j =!(i == k), j = i = 1 12

Expression Boolean Expression Assignment Expression j == i,!i j =!(i == k), j = i = 1... 12

Statement i = 1; {... if(...) { else { while(...) { for(...) { 13

Statement Expression + ; Block If Statement While Statement For Statement i = 1; {... if(...) { else { while(...) { for(...) { 13

Primitive Data Types 14

Primitive Data Types Integers byte, short, int, long. Floating points float, double. Characters char Boolean boolean void 14

Arrays <TYPE> name[][]...[] = new <TYPE>[SIZE][SIZE]...[SIZE]; int onedimarr[] = new int[20]; int threedimarr[][][] = new int[10][20][30]; int[] onedimarr2 = new int[20]; int sizeofarr = threedimarr.length; 15

Arrays Arrays are very similar to C. The main difference is in memory allocation. There are two syntax for array definition. The length property contains the size of your array. <TYPE> name[][]...[] = new <TYPE>[SIZE][SIZE]...[SIZE]; int onedimarr[] = new int[20]; int threedimarr[][][] = new int[10][20][30]; int[] onedimarr2 = new int[20]; int sizeofarr = threedimarr.length; 15

String String str = abcd ; str = str + abcde ; str = 1 + abcd ; 16

String String is a type in Java. String literals are similar to C. We have talked about the + operator. String str = abcd ; str = str + abcde ; str = 1 + abcd ; It can concatenate anything to String. 16

Arithmatic Operators 17

Arithmatic Operators + : Add - : Subtract / : Divide * : Multiply % : Modulus ++ : Increment -- : Decrement += : Addition Assignment -= : Subtraction Asg. *= : Multiplication Asg. /= : Division Asg. %= : Modulus Asg. 17

Bitwise Operators 18

Bitwise Operators ~ : NOT & : AND : OR ^ : XOR << : Shift Left &= : AND Assignment = : OR Assignment ^= : XOR Assigment >> : Shift Right >>> : Shift Right Zero Fill 18

Boolean Operators 19

Boolean Operators & : Logical AND : Logical OR ^ : Logical XOR : Short circuit OR && : Short circuit AND! : Logical NOT &= : AND Assignment = : OR Assignment ^= : XOR Assignment == : XOR Assignment!= : Not equal to? : : Ternary if-then-else 19

Warning (x!= 0) & (1/x!= 2) (x!= 0) && (1/x!= 2) 20

Warning If X is Zero, you will get DivisionByZero error from (x!= 0) & (1/x!= 2) But, you won t get the error from (x!= 0) && (1/x!= 2) 20

Control Statements 21

Control Statements If statement While statement For statement Do-While statement Switch statement return, break, continue 21

if... else... if ( x == 1 ) System.out.println( x was 1 ); else { System.out.println( x was not 1 ); 22

if... else... if (boolean-expr) stmt else stmt if ( x == 1 ) System.out.println( x was 1 ); else { System.out.println( x was not 1 ); 22

while... while(x > 0) System.out.println( x is + x--); 23

while... while (boolean-expr) stmt while(x > 0) System.out.println( x is + x--); 23

for... for (int i = 0; i < 100; i++) System.out.println( i is + i); 24

for... for(inital-stmt; conditions; stmts) stmt for (int i = 0; i < 100; i++) System.out.println( i is + i); 24

do... while... do System.out.println( i is + i); while (i-- > 0); 25

do... while... do stmt while(boolean-expr); do System.out.println( i is + i); while (i-- > 0); 25

switch... case... switch ( i ) { case 1: System.out.println( one ); break; case 2: System.out.println( two ); break; default: System.out.println( I cannot count more than two ); 26

switch... case... switch(expr) { exp1: stmt; exp2: stmt... switch ( i ) { case 1: System.out.println( one ); break; case 2: System.out.println( two ); break; default: System.out.println( I cannot count more than two ); 26

return, break, continue return 1; for (;;) { continue; for (;;) { break; 27

return, break, continue return return 1; continue Sets the return value of a function and exit from it. break Jumps out of a block. Jumps to the beginning of a block. for (;;) { continue; for (;;) { break; 27

Goto in Java first: { second: { third: { System.out.println( B efore 3rd ); break second; System.out.println( 3 rd ); System.out.println( sec ond ); System.out.println( firs t ); 28

Goto in Java break <label>; Break can be used to jump out of a block with the label. first: { second: { third: { System.out.println( B efore 3rd ); break second; System.out.println( 3 rd ); System.out.println( sec ond ); System.out.println( firs t ); 28

Some Useful Functions 29

Some Useful Functions Integer.parseInt() converts a string to an integer. Integer.parseInt( 1 ) == 1 is true Double.parseDouble() converts a string to a double. Double.parseDouble( 1.1 ) == 1.1 is true. 29

Any Questions?