Java Syntax Primitive data types Operators Control statements.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Air Force Institute of Technology Electrical and Computer Engineering
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Some basic I/O.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Graphical User Interface Programming Dr. Susan McKeever (not me)
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
CSC 212 Object-Oriented Programming and Java Part 2.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Pointers *, &, array similarities, functions, sizeof.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Java Primer 1: Types, Classes and Operators
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Selenium WebDriver Web Test Tool Training
Computing with C# and the .NET Framework
C Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Advanced Programming Behnam Hatami Fall 2017.
Starting JavaProgramming
null, true, and false are also reserved.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Recap Week 2 and 3.
elementary programming
Java Programming Language
Java Basics Data Types in Java.
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Java Syntax Primitive data types Operators Control statements

Primitive data types Identical across all computer platforms  portable

Primitive data types char (16 bits) a Unicode character byte (8 bits) int (32 bits) a signed integer short (16 bits) a short integer long (64 bits) a long integer

Primitive data types float (32 bits) a real number double (64 bits) a large real number boolean (8 bits) –values are true or false (keywords) –cannot be converted to and from other data types e.g. while (i!=0) not while(i)

Operators Additive + - Multiplicative * / % Equality == != Assignment operators = += -= *= /= %=

Operators Relational operators >= Increment operators (postfix and prefix) Conditional operator ?: String concatenation +

Logical Operators Not ! Logical AND && Logical OR || Boolean logical AND& Boolean logical inclusive OR| Boolean logical exclusive OR^ (true if only one operand is true)

Control Statements Similar to C/C++ syntax: –if statement if (x != n){ … } else if { … } else { … } –for statement for (int i=0; i<max; i++){ … };

Control Statements –while statement while (x==n ){ … }; –do statement do { … } while( x<=y && x!=0);

Control Statements –switch statement switch (n){ case 1: … break; case 2: case 3: … break; default: break; };

General Points to Note Case sensitive Use lower case –objects have capitalised first letter

Java Reference Types Classes Arrays

Reference Types Classes and arrays are composite types –no standard size –contain other elements Manipulated “by reference’’ to the object or array Primitive data types manipulated “by value”

Reference vs Primitive Types A reference is a value that refers to the object or array A primitive datatype holds the value directly Difference to primitive types effects the way values are copied and compared Setting Object A = Object B only sets the reference and does not set the contents Comparing Object A and Object B, A will not be equal to B even if they have the same contents

References in Java Note: –Java does not support the & address-of or -> and * de-reference operators of C and C++ –the. operator in Java is more like the -> operator of C++ –references in Java cannot be manipulated (e.g. incremented or decremented)

null –is a special value indicating a reference to nothing –can be assigned to a variable of any reference type

Arrays in Java Array declaration: type arrayId[] = new type[limit]; type arrayId[] = new type[] {values}; Multi dimensional array: type arrayId[][] =new type[rowlimit][colLimit] Examples: int frequencies[]= new int[20]; String countryCode[]= new String[176]; double table[]=new double[4][5]; Times timePeriods[]=new Times[8];

Arrays in Java Arrays can be formed from any data type or class Arrays are indexed from 0. Arrays are fixed size but the size can be allocated at run time: e.g.int array1[];// declare array … … int size=n;// get array size, array1 = new int [size];// allocate array Assigning one array to another array copies the reference and does not copy full array.

Arrays in Java Accessing an array element that does not exist will result in an error The length of an array can be accessed using a read-only property called length that is associated with every array. e.g. for (i=0; i<frequencies.length; i++)… Arrays can be passed as parameters to methods e.g. main (String [] args)

Class & Array Interaction There are 3 ways that classes and arrays can interact (1) An array of a class of objects: Student students[] =new Student[8]; (2) A class containing an array and methods that act on it (3) A class containing methods that operate on array parameters

Class & Array Interaction (2) class IntArray { private int arrayOfInt[]; IntArray(int size){ //constructor arrayOfInt=new int [size]; } int get(int index){ // get value at index return arrayOfInt[index]; } void set (int index, int value){ //set value arrayOfInt[index]=value; } public String toString(){ // print out array String s =””; for (int i=0; i<arrayOfInt.length;i++) s += “ “+arrayOfInt[i]; return s; } }

Class & Array Interaction (3) class ArrayUtilities { static int max( int arrayA [] ) { // finds the max element of arrayA // and returns it... return arrayA[i]; } static void sort (int [] arrayA) { //sorts the elements of arrayA... } }

instanceOf operator Used only with arrays and objects, not primitive types value instanceOf referenceType –returns true if the object or array on left is an instance of the type on the right –returns false otherwise –Used for checking types before casting (see later - working with inheritance) “My string” instanceOf String // true “” instanceOf string // true null instanceOf String// false