By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program 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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Road Map Introduction to object oriented programming. Classes
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.
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.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Review Java.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
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.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
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.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
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 Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 3 Introduction to Classes and Objects Definitions Examples.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Written by: Dr. JJ Shepherd
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
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.
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];
Methods.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
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.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Objects as a programming concept
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
An Introduction to Java – Part I
CMSC 202 Static Methods.
Chapter 3 Introduction to Classes, Objects Methods and Strings
An Introduction to Java – Part I, language basics
Classes and Objects 5th Lecture
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
Classes and Objects Static Methods
Object Oriented Programming in java
Names of variables, functions, classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

By Nicholas Policelli An Introduction to Java

Basic Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods }

Basic Program Structure Classes can be public or private Methods can be either public or static Public methods can be called from any class Static methods cannot be used on objects Void indicates that the method does not return a value The array args stores command line arguments

Basic Program Structure The source code must have the same name as the public class and have a.java During compilation the Java compiler produces a bytecode file which is given a.class extension Execution: java (class name)

Data Types Integers: int, short, long, byte Floating-Point Types: float, double The Character Type: char The Boolean Type: boolean (values: true, false)

Operators and Strings Arithmetic Operators: +, -, *, /, % Relational Operators: ==, !=,, = Logical Operators: &&, ||, ! Strings are standard Java class. Concatenation uses +

Variables Variables in Java need to be declared. You need to initialize variables before you use them. Examples: int n=5; System.out.println(n); String s=”Hello”; String t=”world”; System.out.println(s+” “+t);

If, If Else, For,and While Loops If and if else follow the C++ conventions. if(…) { … } While and For loops also follow the same C++ conventions

Arrays Standard class in Java Declared by specifying the type enclosed in [] All elements may be listed by enclosing in {} and separated with commas. If one array variable is copied into another array variable, both variables refer to the same array As in C++ and Perl array indices run from 0 up to the length of the array minus one arrayName.length can be used to find the length of the array

Array Example int n=5; int[] numbers = new int[n]; for(int i=0; i<numbers.length; i++) numbers[i]=n-i; Arrays.sort(numbers); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]);

Object Oriented Programming in Java Program structure with user defined classes The basic structure of Java programs with user defined classes: public class ClassName { public static void main(String[] args) { program statements } user defined methods } user defined classes

Class Definitions The basic structure of Java classes: class NameOfClass { constructors methods fields }

Objects and Object Variables Java constructors construct and initialize objects. Constructors have the same name as the class. Objects are constructed by an application of a constructor and new. Object variables store the reference to the object, not the object itself. Example: Date D=new Date(); // An object of a Date class is constructed and its reference System.out.println(D); // is stored inside variable D. Next we print out the object.

Class Fields and Methods Class fields define the state of an object. Constructors define and initialize class fields. Accessor methods access class fields of an object. Mutator methods are used to modify class fields.

Parameter Passing Two kinds of method parameters Primitive Types Object References In both cases the variable is passed by value, For primitive types the parameter will contain the primitive type For Object References the parameter will contain the reference, not the object

References Example classes and more can be found in the Java resources on the course website