Core Concepts.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
Road Map Introduction to object oriented programming. Classes
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
UMBC CMSC 331 Java JAVA BASICS. UMBC CMSC 331 Java 2 Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc”
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming Languages and Paradigms Object-Oriented Programming.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
CS 363 Spring 2004 Elizabeth White CS 363 Comparative Programming Languages 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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Constructors & Garbage Collection Ch. 9 – Head First Java.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Memory Management in Java Mr. Gerb Computer Science 4.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
The Object-Oriented Thought Process Chapter 03
Topic: Classes and Objects
JAVA METHODS and CONSTRUCTORS
CSE 374 Programming Concepts & Tools
Static data members Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
CSE 413, Autumn 2002 Programming Languages
Chapter 3: Using Methods, Classes, and Objects
CS230 Tutorial Week 3.
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ -> Java - A High School Teacher's Perspective
CSC 143 Inheritance.
CSC 113 Tutorial QUIZ I.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
JAVA BASICS.
Lecture 03 & 04 Method and Arrays Jaeki Song.
JAVA BASICS.
String Class.
Review for Midterm 3.
CSC 533: Programming Languages Spring 2019
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Core Concepts

Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc” style comments in your code. /* This kind comment can span multiple lines */ // This kind is of to the end of the line /* This kind of comment is a special * ‘javadoc’ style comment */

JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and the operations (or methods) that can be applied to those objects Every object and method in Java belongs to a class Classes have data (fields) and code (methods) and classes (member classes or inner classes) Static methods and fields belong to the class itself Others belong to instances

An example of a class class Person { Variable String name; int age; Method void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } }

Scoping As in C/C++, scope is determined by the placement of curly braces {}. A variable defined within a scope is available only to the end of that scope. { int x = 12; /* only x available */ { int q = 96; /* both x and q available */ } /* q “out of scope” */ This is ok in C/C++ but not in Java. { int x = 12; { int x = 96; /* illegal */ }

Scope of Objects Java objects don’t have the same lifetimes as primitives. When you create a Java object using new, it hangs around past the end of the scope. Here, the scope of name s is delimited by the {}s but the String object hangs around until GC’d { String s = new String("a string"); } /* end of scope */

The static keyword Java methods and variables can be declared static These exist independent of any object This means that a Class’s static methods can be called even if no objects of that class have been created and static data is “shared” by all instances (i.e., one rvalue per class instead of one per instance class StaticTest {static int i = 47;} StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); // st1.i == st2.I == 47 StaticTest.i++; // or st1.I++ or st2.I++ // st1.i == st2.I == 48

Array Operations Subscripts always start at 0 as in C Subscript checking is done automatically Certain operations are defined on arrays of objects, as for other classes e.g. myArray.length == 5

An array is an object Person mary = new Person ( ); int myArray[ ] = new int[5]; int myArray[ ] = {1, 4, 9, 16, 25}; String languages [ ] = {"Prolog", "Java"}; Since arrays are objects they are allocated dynamically Arrays, like all objects, are subject to garbage collection when no more references remain so fewer memory leaks Java doesn’t have pointers!

Example Programs

Constructors Classes should define one or more methods to create or construct instances of the class Their name is the same as the class name note deviation from convention that methods begin with lower case Constructors are differentiated by the number and types of their arguments An example of overloading If you don’t define a constructor, a default one will be created. Constructors automatically invoke the zero argument constructor of their superclass when they begin (note that this yields a recursive process!)

Methods, arguments and return values Java methods are like C/C++ functions. General case: returnType methodName ( arg1, arg2, … argN) { methodBody } The return keyword exits a method optionally with a value int storage(String s) {return s.length() * 2;} boolean flag() { return true; } float naturalLogBase() { return 2.718f; } void nothing() { return; } void nothing2() {}