An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.

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

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Introduction to Programming G51PRG University of Nottingham Revision 1
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
1 Chapter 4: Arrays and strings Taufik Djatna
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.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
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.
Chapter 8: Arrays.
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.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
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.
Introduction to Java Java Translation Program Structure
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
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++
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
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: รัฐภูมิ เถื่อนถนอม
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
Java Primer 1: Types, Classes and Operators
Selenium WebDriver Web Test Tool Training
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
An Introduction to Java – Part I
Chapter 2.
IDENTIFIERS CSC 111.
An Introduction to Java – Part I, language basics
Chapter 2: Java Fundamentals
Arrays in Java.
Names of variables, functions, classes
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010

Some Java Basics Derives most of its syntax from C++ Built almost exclusively as an object- oriented programming language All code is written inside a class Everything is an object, with the exception of the intrinsic data types

Data Types (also called primitive types) Byte –Min value: -128 –Max value: 127 –8 bits (used for memory saving purposes) Short –Min value: -32,768 –Max value: 32,767 –16 bits Int –Min value: -2,147,483,648 –Max value: 2,147,483,647 –32 bits –For integral types, this data type is generally the default Long –Min value: -9,223,372,036,854,775,808 –Max value: 9,223,372,036,854,775,807 –64 bits –Use this only when you need a range of values wider than those provided by int

Data Types (also called primitive types) Float –Use a float (instead of a double) if you need to save memory –Single precision, 32 bit Double –For decimal values this is the default choice –Double precision, 64 bit Boolean –Two possible values: true and false Char –A single character

Arrays A container object that holds a fixed number of values of a single type Length is established when the array is created. After creation, length is fixed. Declaration: int[] anArray; // declares an array of integers The declaration does not actually create an array. It simply tells the compiler that this variable will hold an array of the specified type.

Arrays cont’d You can also declare an array like this: int anArray[]; //this form is discouraged However, convention discourages this form; the brackets identify the array type and should appear with the type designation. int[] anArray;//declares an array of integers anArray = new int[10]; //allocates memory for 10 integers Alternatively you can use the shortcut syntax to create and initialize an array int[] anArray = {1,2,3,4,5,6,7,8,9,10}; Here the length of the array is determined by the number of values between { and }.

Arrays cont’d Elements of an array are accessed using subscripts (just like in C++ and Perl) int[] anArray; //declares an array of integers anArray = new int[10];//allocates memory for 10 integers anArray[0] = 1;//initialize first element anArray[1] = 2;//Initialize second element System.out.println(“Element at index 0: “ + anArray[0]); System.out.println(“Element at index 1: “ + anArray[1]);

Selected Member Functions for Arrays Static void sort(type[] a) –Sorts an array using a QuickSort algorithm Static int binarySearch(type[] a, type v) –Argument a is a sorted array. This function searches for v among the elements of a and returns subscript if v is found. Static boolean equals(type[] a, Object other) –This function returns true if other is an array of the same type as a, if it has the same length, and if the corresponding elements match

Strings Strings are a standard class in Java String str1; //declare a string variable str1 = “Erin”; //initialize string variable String str2 = “My name is” //declaration and initialization in one line String concatenation: + String str3 = str2 + “ “ + str1 + “.”; //str3 holds the string “My name is Erin.”

Selected Member Functions for Strings int compareTo(String other) –Returns a negative value if the implicit argument comes before the explicit argument, a positive value if the explicit argument comes before the implicit argument, and 0 if the strings are equal int cmp = impStr.compareTo(expStr); boolean equals(Object other) –Returns true if the implicit argument equals the explicit argument. Returns false otherwise. boolean eq = impStr.equals(expStr); int length() –Returns the length of the string int length = impStr.length();

Operators Arithmetic Operators +, -, *, /, % Relational Operators ==, !=,, = Logical Operators &&, ||, !

Control Flow If else statements –Same as in C++ –Curly braces are unnecessary if they only enclose one statement While loops –Same as C++ –Curly braces are unnecessary if they only enclose one statement For loops –Same as C++ for(int i=0; i<5; i++) System.out.println(numbers[i]);

Program Structure Public class className { public static void main(String[] args) { program statements } user-defined methods } Public classes are accessible from any class Classes that are not declared public are accessible only within their package –(A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. ) Public methods may be called from any class Static methods do not operate on objects The keyword void indicates that the method does not return a value The array args of the main method stores command line arguments

public class SortJava { public static void main(String[] args) { int n=5; int[] numbers = new int[n]; //The Math.random() method returns a random floating oint number in the range //[0,1). We multiply the random number by n and then we cast the type of the result //to integer. for(int i=0; i<numbers.length; i++) numbers[i]=(int)(Math.random()*n); System.out.println("Random numbers before sorting"); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); System.out.println(); System.out.println("Random numbers after sorting"); // We call sort method of Arrays class. // Array class is located in java.util.Arrays. Import //statement java.util.* allows us to access it. Arrays.sort(numbers); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); }

Source Code, Compilation, Execution Source code needs to have same as name of public class –There can only be one public class in a Java program Source code needs to have extension.java During compilation, the Java compiler produces a bytecode file –This file has the extension.class The Java interpreter starts execution when we type command java followed by the class name

Questions?