Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Size: px
Start display at page:

Download "Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline"

Transcription

1 Java Intro 3 9/7/ Java Intro 3 Outline Java API Packages Access Rules, Class Visibility Strings as Objects Wrapper classes Static Attributes & Methods Hello World details 9/7/ Class Libraries and the Java API 9/7/

2 Java API The Java programming language is defined by: 1. The essential features of the language (syntax) 2. A set of standard classes (e.g. String) used by most implementations of the language 3. A set of classes provided with an environment called the Java API (application programming interfaces) 9/7/ Java API Most of the Java API is written in terms of primitive language features. Some parts of the Java API are written in terms of the native operating system. 9/7/ The following program uses both essential language features and the API import java.lang.string; import java.io.filewriter; import java.io.ioexception; Qualified names public class test { public static void main(string[] args) { Standard classes try { FileWriter fw = new FileWriter("hello.txt"); String h = "Hello"; String w = "World"; fw.write(h + " " + w); fw.close(); catch (IOException e) { System.out.println("Error writing to file:" + e); Essential Language Features 9/7/

3 The Java API is organized into groups of related classes and interfaces called packages. A package defines a name space. The Java API (version 1.4.2) includes about 130 packages. To place a class inside a package, you add a package statement as the first statement of the source file package IT350program1 public class Student { 9/7/ Java Packages A class without a package statement is in the default package with no package name The full name of a class includes the package name followed by class name. For example: java.util.arraylist javax.swing.joptionpane 9/7/ Common Java Packages java.lang Includes Integer, String, StringBuffer, System, Runtime, etc. These are fundamental classes. Some of the classes even have support in the compiler and JVM. It is not necessary to import this package into the programs you write. This package is included automatically. 9/7/

4 Common Java Packages java.util Includes miscellaneous utility classes. includes collection classes such as Vector, Stack and LinkedList and other useful classes such as StringTokenizer andrandom. java.io Includes input and output classes. Two main categories: (1) streams (byte oriented I/O), and (2) readers and writers (Unicode or character I/O). 9/7/ Common Java Packages java.awt Includes original user interface control classes such as Button and TextField. Swing components have replaced some of the classes in this package, but most of the fundamental graphics classes in this package are still used. javax.swing The "swing" classes were added in Java 2. The swing package includes new classes such as JButton, JTextField, JApplet etc. java.applet Includes the Applet class and a few interfaces which are used to create applets. 9/7/ Importing Java Packages import all the classes in a package by using the wildcard character (*) import java.util.* //comment which class is being used The wildcard pertains only to class names for example, import classa.classb.*; will only import classes in the classa.classb package and NOT classes in a classa.classb.classc package, if there is one Or specify individual classes from a package: import.java.util.vector; This is the recommended style import.java.util.date; 9/7/

5 Importing Java Packages If you have several files for an application, you can use them all without writing import statements if they are in the same directory. Import statements are only needed if we are using classes that are not found in package java.lang and are not in our own (default) package. 9/7/ Feature Access Rules Access or visibility rules determine whether a method or a data variable can be accessed by another method in another class or subclass Instance variables & methods are contained in classes, which are contained in packages. To determine whether a field or method is accessible, Java starts by determining whether its containing package is accessible and then whether its containing class is accessible. 9/7/ Access Example public class Person{ private String ssn; //the only really private feature //the following attributes are public from the //perspective of other classes in the same package public String name; protected String address; int age; 9/7/

6 Visibility Making a class public does not have any effect on the visibility of its features. Private features are still private protected features are still protected 9/7/ Class Visibility Class access is public by default accessible if its package is accessible accessible only within its package. Declaring a class public means: if the package the class belongs to is imported into code belonging to some other package, then all public attributes and methods are public in the other package. (e.g. public class Vector in java.util) Features of a public class still have the same meaning. Private feature is still private, etc. 9/7/ Class Visibility If access designation is omitted from the class declaration, the class is package visible. The class is not visible from outside the package and cannot be referenced or instantiated from client code within another package. Class visibility is a form of encapsulation at the package level. Two or more classes can be placed in a single.java file (not recommended), but only one of the classes can be public. The public class s name must match the name of the file. 9/7/

7 Accessibility of Class Members public protected package private The class itself Yes Yes Yes Yes Classes in the Yes Yes Yes No same package Subclasses in a Yes Yes No No different package Non-subclasses in Yes No No No a different package 9/7/ Revisiting Strings as Objects Strings are created in two ways: String name = Judy ; String name = new String( Judy ); in the first case, the string is stored in a literal pool which is shared if you use the same String literal in different places in the code. in the second case, a different instance of the string is created with every call to new, even if there is already a string with the same literal value in the application. 9/7/ String class methods Besides length(), there are other useful methods in the String class: String s = banana split ; if(s.startswith( ba )) //evaluates to true if(s.endswith( it )) //evaluates to true int i = s.indexof( nan ); //i will equal 2 String j = it ; int k = s.indexof(j); //k will equal 10; System.out.println(s.indexOf( spil ); //prints -1 9/7/

8 String class methods String replace(char old, char new); String s = x1 x2 x3 ; String y = s.replace( x, y ); String s is unchanged; String y is y1 y2 y3 String substring(int); String s = banana split ; String t = s.substring(7); //t is now reference to split String substring(int, int); String s = banana split ; String t = s.substring(9,12); //t is reference to lit 9/7/ String objects Strings are immutable. When you think you are modifying a String object by concatenating another String object to it, you are actually creating a new string object -- making the old String object inaccessible by reference. String s = banana ; s = s + split ; banana bananasplit s no longer accessible s 9/7/ Wrapper Classes The wrapper classes are primarily used to store primitive data types in objects so they can be handled by classes that only work with objects. For example, the Java Collections API has many data structure classes such as ArrayList and HashMap that only work with objects in order to store a primitive number in one of these collections, you must first wrap it. 9/7/

9 Wrapper Classes A wrapper class is provided for each primitive data type. Wrappers encapsulate methods that can be used to perform certain data conversions. The wrapper class integer has a method parseint() that converts a String into an int: int x = parseint( 350 ); parseint() is used to handle numeric input Wrapper classes include Boolean, Character, Byte, Short, integer,long, Float and Double 9/7/ Wrapper Classes Storing integers in a Vector object Simple data types cannot be stored in a container object because containers hold Object type elements, and simple data types are not Object types. To store integers in a Vector object, we build an object wrapper around each int value and add the object wrapper to the vector instead of the int itself. v.add(new Integer(age)); //wrap age in Integer 9/7/ Autoboxing Syntact shortcut added to java 5.0 autoboxing -- automatically converting primitives to wrappers To autobox, just assign a primitive value to an appropriate wrapper object auto-unboxing -- automatically converting wrappers to numbers To auto-unbox, assign a wrapper object reference to an appropriate primitive variable Integer a = 37; //autobox int to Integer float f = new Float(15.5f); //auto-unbox Float to float long l = a; //auto-unbox Integer to long 9/7/

10 Using Wrapper Classes to Store integers in a Vector object //code by Jacquie Barker, modified by Judy Mullins import java.util.* //for Vector public class VectorTest { public static void main(string[] args) { Vector v = new Vector(); //container of integers for (int i = 0; i < 10; i++) { Integer intwrapper = new Integer(i); v.add(intwrapper); for (int i = 0; i<10; i++) { Integer intwrapper = (Integer) v.elementat(i); Sysem.out.println ( intwrapper.intvalue() ); 9/7/ Using Autoboxing in a Vector object //code by Judy Mullins import java.util.* //for Vector public class VectorTest { public static void main(string[] args) { Vector<Integer> v = new Vector<Integer>(); //container of integers for (int i = 0; i < 10; i++) { v.add(i); for(int value : v) System.out.println(value); 9/7/ Converting bet. numbers and strings int Integer.parseInt(String); converts the String argument to an int if the argument is a valid integer, else a NumberFormatException is thrown: public class integertest{ public static void main(string [] args) { String[] ints = { 123, 456, hello ; try { for (int i=0; i < ints.length; i++) { int test = Integer.parseInt(ints[i]); System.out.println( test + converted ok ); catch (NumberFormatException e) { System.out.println( ints[i] + is invalid integer ); 9/7/

11 Converting bet. numbers and strings String Integer.toString(int) turns an integer into a String int j = 9; String s = Integer.toString(j); //s equals 9 Easier way to do the same thing: int j = 9; String s = + j;//concatenate empty string with int. //The int will automatically be // changed to a String 9/7/ Class Modifiers public Accessible everywhere. One public class allowed per file. The file must be named ClassName.java private Only accessible within a file <empty> Accessible within the current class package. abstract A class that contains abstract methods final No subclasses 9/7/ Method and Field Modifiers For both methods and fields public protected private static final For methods only abstract synchronized native For fields only volatile transient 9/7/

12 final Variables Final variables are named constants. class CircleStuff { static final double pi =3.1416; Final variables are also static. 9/7/ Hello World Revisited Details about this little program 9/7/ Hello World /* 1 */ // My first Java program /* 2 */ import java.lang.*; //optional import /* 3 */ public class HelloWorld { /* 4 */ public static void main(string[] args) { /* 5 */ System.out.println("Hello World!"); /* 6 */ /* 7 */ Note: Line numbers are NOT part of the java program. 9/7/

13 Class containing main() method is considered the application driver Entry point for the class: public static void main(string[] args) The class name must match exactly (i.e. case) the name of the external file containing the source code of the class. 9/7/ The main() method has two responsibilities 1. Instantiate the core objects needed for the program 2. Display the start-up window of the GUI of an application, if it has one 9/7/ Why is the main() method declared static? A static method can be invoked on a class even if no instance of the class has been created. When the JVM loads the class with the main method, no objects exist yet because the main() method, which starts the instantiation process, hasn t been executed yet! After executing main(), the JVM loads additional classes as needed when referenced by the application. 9/7/

14 Static Methods A static method (class method) can be invoked on either an instance or a class as a whole. public class Student { public static int totalstudentcount; public static void incrementtotalcount() { totalstudentcount++;) //in main program: Student.incrementTotalCount(); Student s1=new Student(); s1.incrementtotalcount(); 9/7/ Static Methods A static method can only access static attributes and invoke other static methods The this keyword has no meaning within a static method we can t know for sure that there will be an instance of an object to self-reference 9/7/ Static Attributes A static attribute is shared by all instances of a class. It does not belong to any one instance or object of the class for which it is declared. one per class, rather than one per object. Counters are often declared static public class Student { public static int totalstudentcount = 0; //opt. Initialization 9/7/

15 Static Attributes When any object of the class modifies a static attribute, the new value applies to all objects Student s1 = new Student(); Student s2 = new Student(); s1.totalstudentcount++; class s2.totalstudentcount++; // 1 student for the // now 2 students 9/7/ Static Attributes The value of the static attribute can be accessed by the class using the dot notation Student.totalStudentCount++; System.out.println(Student.totalStudentCount); Class name Class name 9/7/ Static Attributes Static attributes are also called class variables or instance variables Common usage: to make data globally available to an application Class variables are uncommon more common is to define constants public static final double PI = /7/

16 Class Declaration Example public class Point { public int x, y; public void move(int dx, int dy) { x += dx; y += dy; Point point1; // Point not created Point point2 = new Point(); point1 = point2; point1 = null; x and y are initialized to their default initial values. 9/7/ Explicit Initializer public class Point { public int x = 0, y = 0; public void move(int dx, int dy) { x += dx; y += dy; Point point1 = new Point(); // (0,0) 9/7/ Constructors public class Point { public int x, y; public Point() { // no-arg x = 0; y = 0; public Point(int x0, int y0) { x = x0; y = y0; Point point1 = new Point(); // no-arg Point point2 = new Point(20, 20); Constructors are invoked after default initial values are assigned. No-arg constructor is provided as a default when no other constructors are provided. 9/7/

17 Resources for further reading About Java language features: About Java technology: efinition.html Quick introduction to Java programming: html All the language fundamentals: 9/7/

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

JAVA WRAPPER CLASSES

JAVA WRAPPER CLASSES JAVA WRAPPER CLASSES Description Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of

More information

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B 1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these 2. How many primitive data types are there in Java? A. 5 B. 6 C. 7 D. 8 3. In Java byte, short, int and long

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries 1 CONTENTS 1. Introduction to Java 2. Holding Data 3. Controllin g the f l o w 4. Object Oriented Programming Concepts 5. Inheritance & Packaging 6. Handling Error/Exceptions 7. Handling Strings 8. Threads

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE

More information

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748 COT 3530: Data Structures Giri Narasimhan ECS 389; Phone: x3748 giri@cs.fiu.edu www.cs.fiu.edu/~giri/teach/3530spring04.html Evaluation Midterm & Final Exams Programming Assignments Class Participation

More information

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

The Sun s Java Certification and its Possible Role in the Joint Teaching Material The Sun s Java Certification and its Possible Role in the Joint Teaching Material Nataša Ibrajter Faculty of Science Department of Mathematics and Informatics Novi Sad 1 Contents Kinds of Sun Certified

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Formatting Output & Enumerated Types & Wrapper Classes

Formatting Output & Enumerated Types & Wrapper Classes Formatting Output & Enumerated Types & Wrapper Classes Quick review of last lecture September 8, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p. A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p. 9 Classes and Objects p. 11 Creating Objects p. 12 Static or

More information

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA 1. JIT meaning a. java in time b. just in time c. join in time d. none of above CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA 2. After the compilation of the java source code, which file is created

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Class Libraries and Packages

Class Libraries and Packages Class Libraries and Packages Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Packages. Examples of package names: points java.lang com.sun.security drawing.figures

Packages. Examples of package names: points java.lang com.sun.security drawing.figures Packages To make classes easier to find and to use, to avoid naming conflicts, and to control access, programmers bundle groups of related classes and interfaces into packages. A package is a program module

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Java Language Features

Java Language Features Java Language Features References: Object-Oriented Development Using Java, Xiaoping Jia Internet Course notes by E.Burris Computing Fundamentals with Java, by Rick Mercer Beginning Java Objects - From

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CT 229 Fundamentals of Java Syntax

CT 229 Fundamentals of Java Syntax CT 229 Fundamentals of Java Syntax 19/09/2006 CT229 New Lab Assignment Monday 18 th Sept -> New Lab Assignment on CT 229 Website Two Weeks for Completion Due Date is Oct 1 st Assignment Submission is online

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Packages & Random and Math Classes

Packages & Random and Math Classes Packages & Random and Math Classes Quick review of last lecture September 6, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Objects Classes An object

More information

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents character strings. All string literals in Java programs,

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

Using Classes and Objects. Chapter

Using Classes and Objects. Chapter Using Classes and Objects 3 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Using Classes and Objects To create

More information

Preview from Notesale.co.uk Page 9 of 108

Preview from Notesale.co.uk Page 9 of 108 The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names. abstract assert boolean break byte case catch char class

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

CST141 Thinking in Objects Page 1

CST141 Thinking in Objects Page 1 CST141 Thinking in Objects Page 1 1 2 3 4 5 6 7 8 Object-Oriented Thinking CST141 Class Abstraction and Encapsulation Class abstraction is the separation of class implementation from class use It is not

More information

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double(3.14); 4... Zheng-Liang Lu Java Programming 290 / 321 Wrapper Classes To treat values as objects, Java supplies standard wrapper classes for each primitive type. For example, you can construct a wrapper object from a primitive value or from a string representation

More information

5/23/2015. Core Java Syllabus. VikRam ShaRma

5/23/2015. Core Java Syllabus. VikRam ShaRma 5/23/2015 Core Java Syllabus VikRam ShaRma Basic Concepts of Core Java 1 Introduction to Java 1.1 Need of java i.e. History 1.2 What is java? 1.3 Java Buzzwords 1.4 JDK JRE JVM JIT - Java Compiler 1.5

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Timing for Interfaces and Abstract Classes

Timing for Interfaces and Abstract Classes Timing for Interfaces and Abstract Classes Consider using abstract classes if you want to: share code among several closely related classes declare non-static or non-final fields Consider using interfaces

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4. 1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net COURSE ORGANIZATION 2 Course Elements Lectures: 10 lectures Find schedule and class rooms in online

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

From C++ to Java. Duke CPS

From C++ to Java. Duke CPS From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed on JVM byte-code is highly-portable, write

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

PIC 20A The Basics of Java

PIC 20A The Basics of Java PIC 20A The Basics of Java Ernest Ryu UCLA Mathematics Last edited: November 1, 2017 Outline Variables Control structures classes Compilation final and static modifiers Arrays Examples: String, Math, and

More information

20 Most Important Java Programming Interview Questions. Powered by

20 Most Important Java Programming Interview Questions. Powered by 20 Most Important Java Programming Interview Questions Powered by 1. What's the difference between an interface and an abstract class? An abstract class is a class that is only partially implemented by

More information

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

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

S.E. Sem. III [CMPN] Object Oriented Programming Methodology S.E. Sem. III [CMPN] Object Oriented Programming Methodology Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 80 Q.1(a) Write a program to calculate GCD of two numbers in java. [5] (A) import java.util.*;

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Programming in JAVA II (A Whirlwind Tour of Java) Course Lecture Slides 17 May 2010 Ganesh Viswanathan JAVA Java 1.0 was first released by

More information

USING LIBRARY CLASSES

USING LIBRARY CLASSES USING LIBRARY CLASSES Simple input, output. String, static variables and static methods, packages and import statements. Q. What is the difference between byte oriented IO and character oriented IO? How

More information

Java Programming. Atul Prakash

Java Programming. Atul Prakash Java Programming Atul Prakash Java Language Fundamentals The language syntax is similar to C/ C++ If you know C/C++, you will have no trouble understanding Java s syntax If you don't, it will be easier

More information

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure. Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2 CS321 Languages and Compiler Design I Winter 2012 Lecture 2 1 A (RE-)INTRODUCTION TO JAVA FOR C++/C PROGRAMMERS Why Java? Developed by Sun Microsystems (now Oracle) beginning in 1995. Conceived as a better,

More information

Brief Summary of Java

Brief Summary of Java Brief Summary of Java Java programs are compiled into an intermediate format, known as bytecode, and then run through an interpreter that executes in a Java Virtual Machine (JVM). The basic syntax of Java

More information

Interfaces (1/2) An interface forms a contract between the object and the outside world.

Interfaces (1/2) An interface forms a contract between the object and the outside world. Interfaces (1/2) An interface forms a contract between the object and the outside world. For example, the buttons on remote controls for some machine. As you can see, an interface is a reference type,

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003 The University of Melbourne Department of Computer Science and Software Engineering 433-254 Software Design Semester 2, 2003 Answers for Tutorial 7 Week 8 1. What are exceptions and how are they handled

More information

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Objectives of CS 230. Java portability. Why ADTs?  8/18/14 http://cs.wellesley.edu/~cs230 Objectives of CS 230 Teach main ideas of programming Data abstraction Modularity Performance analysis Basic abstract data types (ADTs) Make you a more competent programmer

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2011 PLC 2011, Lecture 2, 6 January 2011 Classes and

More information

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments CMSC433, Spring 2004 Programming Language Technology and Paradigms Java Review Jeff Foster Feburary 3, 2004 Administrivia Reading: Liskov, ch 4, optional Eckel, ch 8, 9 Project 1 posted Part 2 was revised

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70 I,.. CI/. T.cH/C8E/ODD SEM/SEM-5/CS-504D/2016-17... AiIIIII "-AmI u...iir e~ IlAULAKA ABUL KALAM AZAD UNIVERSITY TECHNOLOGY,~TBENGAL Paper Code: CS-504D OF OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3

More information

Another IS-A Relationship

Another IS-A Relationship Another IS-A Relationship Not all classes share a vertical relationship. Instead, some are supposed to perform the specific methods without a vertical relationship. Consider the class Bird inherited from

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Modern Programming Languages. Lecture Java Programming Language. An Introduction Modern Programming Languages Lecture 27-30 Java Programming Language An Introduction 107 Java was developed at Sun in the early 1990s and is based on C++. It looks very similar to C++ but it is significantly

More information

Introduction to Java https://tinyurl.com/y7bvpa9z

Introduction to Java https://tinyurl.com/y7bvpa9z Introduction to Java https://tinyurl.com/y7bvpa9z Eric Newhall - Laurence Meyers Team 2849 Alumni Java Object-Oriented Compiled Garbage-Collected WORA - Write Once, Run Anywhere IDE Integrated Development

More information

Core Java Syllabus. Overview

Core Java Syllabus. Overview Core Java Syllabus Overview Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java

More information

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming

More information

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

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class: Week 8 Variables of class Type - Correction! Libraries/Packages String Class, reviewed Screen Input/Output, reviewed File Input/Output Coding Style Guidelines A simple class: Variables of class Type public

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

Lab5. Wooseok Kim

Lab5. Wooseok Kim Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201 Question Answer Points 1 A 8 2 A 8 3 E 8 4 D 8 5 20 5 for class 10 for main 5 points for output 6 A 8 7 B 8 8 0 15 9 D 8 10 B 8 Question

More information

Java Foundations Certified Junior Associate

Java Foundations Certified Junior Associate Java Foundations Certified Junior Associate 习题 1. When the program runs normally (when not in debug mode), which statement is true about breakpoints? Breakpoints will stop program execution at the last

More information

Programming. Syntax and Semantics

Programming. Syntax and Semantics Programming For the next ten weeks you will learn basic programming principles There is much more to programming than knowing a programming language When programming you need to use a tool, in this case

More information

Methods (Deitel chapter 6)

Methods (Deitel chapter 6) Methods (Deitel chapter 6) 1 Plan 2 Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods

More information

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus PESIT Bangalore South Campus 15CS45 : OBJECT ORIENTED CONCEPTS Faculty : Prof. Sajeevan K, Prof. Hanumanth Pujar Course Description: No of Sessions: 56 This course introduces computer programming using

More information

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Introduction to Java

Introduction to Java Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1 Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn

More information

Table of Contents Fast Track to Java 8 EVALUATIONA COPY

Table of Contents Fast Track to Java 8 EVALUATIONA COPY Table of Contents Fast Track to Java 8 Fast Track to Java 8 and OO Development 1 Course Overview 2 Course Objectives 3 Course Objectives 4 Labs 5 Typographic Conventions 6 Course Outline 7 Session 1 -

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information