Java Programming Language Lecture27- An Introduction.

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

Introduction to C Programming
Chapter 8: Arrays.
Introduction to Programming G51PRG University of Nottingham Revision 1
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Java Syntax Primitive data types Operators Control statements.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
C Tokens Identifiers Keywords Constants Operators Special symbols.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
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.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
August 6, 2009 Data Types, Variables, and Arrays.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Reference Learning Java DDC publishing Herst, Yamauchi, Adler COP 3331 Object Oriented Analysis and Design Java Part I.
1.2 Primitive Data Types and Variables
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Lecture 07. Do not have to create an array while declaring array variable [] variable_name; int [] prime; int prime[]; Both syntaxes are equivalent No.
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.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Windows Programming Lecture 03. Pointers and Arrays.
The Machine Model Memory
Chapter 6 – Data Types CSCE 343.
Chapter 6: Data Types Lectures # 10.
Data types and variables
Chapter 15 Pointers, Dynamic Data, and Reference Types
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Java Programming Language
Presentation transcript:

Java Programming Language Lecture27- An Introduction

A First Look At Java

class HelloWorld { public static void main( String [ ] args ) { System.out.println("Hello world!"); }

In Java, every variable, constant, and function (including main) must be inside some class. No global variables or functions Function main is member of the class. Every class may have a main function; there may be more than one main function in a Java program. The main must be public static void. The main may have one argument: an array of String. This array contains the command-line arguments. There is no final semi-colon at the end of the class definition.

Java Files Source code:.java file Restrictions: 1.Each source file can contain at most one public class. 2.If there is a public class, then the class name and file name must match. Every function must be part of a class Every class is part of a package A public class can be used in any package A non-public class can only be used in its own package.

Java Types Java has two "categories" of types: primitive types and reference types. Primitive Types –All the primitive types have specified sizes that are machine independent for portability. –This includes: boolean same as bool in C++ char holds one 16 bit unicode character byte 8-bit signed integer short 16-bit signed integer int 32-bit signed integer long 64-bit signed integer float floating-point number double double precision floating-point number Reference Types –arrays classes –There are no struct, union, enum, unsigned, typedef, or pointers types in Java.

C++ Arrays vs Java Arrays In C++, when you declare an array, storage for the array is allocated. In Java, when you declare an array, you are really only declaring a pointer to an array; storage for the array itself is not allocated until you use "new". C++ int A[10]; // A is an array of length 10 A[0] = 5; // set the 1st element of array A JAVA int [ ] A; // A is a reference to an array A = new int [10]; // now A points to an array of length 10 A[0] = 5; // set the 1st element of the array pointed to by A

In both C++ and Java you can initialize an array using values in curly braces. Here's example Java code: Example int [ ] myArray = {13, 12, 11}; // myArray points to an array of length 3 // containing the values 13, 12, and 11 In Java, a default initial value is assigned to each element of a newly allocated array if no initial value is specified.

The default value depends on the type of the array element as shown below: TypeValue booleanfalse char'\u0000' byte, int, short, long, float, double0 any referencenull

2-dimensional array in C++ is declared as int[3][4] In Java, two-dimensional arrays need not be rectangular. Each row can be a different length. Here's an example: int [ ][ ] A; // A is a two-dimensional array A = new int[5][]; // A now has 5 rows, but no columns yet A[0] = new int [1]; // A's first row has 1 column A[1] = new int [2]; // A's second row has 2 columns A[2] = new int [3]; // A's third row has 3 columns A[3] = new int [5]; // A's fourth row has 5 columns A[4] = new int [5]; // A's fifth row also has 5 columns

In Java, array bounds are checked and an out-of-bounds array index always causes a runtime error. In Java, you can also determine the current length of an array (at runtime) using ".length" operator as shown below: int [ ] A = new int[10];... A.length... // this expression evaluates to 10 A = new int[20];... A.length... // now it evaluates to 20

In Java, you can copy an array using the arraycopy function. Like the output function println, arraycopy is provided in java.lang.System, so you must use the name System.arraycopy. The function has five parameters: –src: the source array (the array from which to copy) –srcPos: the starting position in the source array –dst: the destination array (the array into which to copy) –dstPos: the starting position in the destination array –count: how many values to copy

Example: int [ ] A, B; A = new int[10]; // code to put values into A B = new int[5]; System.arraycopy(A, 0, B, 0, 5) // copies first 5 values from A to B System.arraycopy(A, 9, B, 4, 1) // copies last value from A into last // element of B Note that the destination array must already exist (i.e., new must already have been used to allocate space for that array), and it must be large enough to hold all copied values (otherwise you get a runtime error). Furthermore, the source array must have enough values to copy (i.e., the length of the source array must be at least srcPos+count).

For arrays of primitive types, the types of the source and destination arrays must be the same. For arrays of non-primitive types, System.arraycopy(A, j, B, k, n) is OK if the assignment B[0] = A[0] would be OK.