NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.

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

Basic Java Constructs and Data Types – Nuts and Bolts
Air Force Institute of Technology Electrical and Computer Engineering
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.
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.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
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”
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
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.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
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.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
CIT 590 Intro to Programming First lecture on Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
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]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Memory Management in Java Mr. Gerb Computer Science 4.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming Language Lecture27- An Introduction.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Topic: Classes and Objects
JAVA METHODS and CONSTRUCTORS
Object-Oriented Programming with Java
Methods Chapter 6.
CSE 413, Autumn 2002 Programming Languages
Object-Oriented Software Engineering
Core Concepts.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
JAVA BASICS.
JAVA BASICS.
Presentation transcript:

NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT

NSIT,Jetalpur Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc” style comments in your code. 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 */

NSIT,Jetalpur JAVA Classes JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) 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 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 Every object and method in Java belongs to a class Classes have data (fields) and code (methods) and classes (member classes or inner classes) Classes have data (fields) and code (methods) and classes (member classes or inner classes) Static methods and fields belong to the class itself Static methods and fields belong to the class itself Others belong to instances Others belong to instances

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

NSIT,Jetalpur 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. 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 */ } /* only x available */ /* q “out of scope” */ } { int x = 12; { int x = 96; /* illegal */ } } This is ok in C/C++ but not in Java.

NSIT,Jetalpur Scope of Objects Scope of Objects Java objects don’t have the same lifetimes as primitives. 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. 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 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"); String s = new String("a string"); } /* end of scope */

NSIT,Jetalpur The static keyword The static keyword Java methods and variables can be declared static Java methods and variables can be declared static These exist independent of any object These exist independent of any object This means that a Class’s 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

NSIT,Jetalpur Example Example public class Circle { // A class field // A class field public static final double PI= ; // A useful constant public static final double PI= ; // A useful constant // A class method: just compute a value based on the arguments // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { public static double radiansToDegrees(double rads) { return rads * 180 / PI; return rads * 180 / PI; } // An instance field // An instance field public double r; // The radius of the circle public double r; // The radius of the circle // Two methods which operate on the instance fields of an object // Two methods which operate on the instance fields of an object public double area() { // Compute the area of the circle public double area() { // Compute the area of the circle return PI * r * r; return PI * r * r; } public double circumference() { // Compute the circumference of the circle public double circumference() { // Compute the circumference of the circle return 2 * PI * r; return 2 * PI * r; }}

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

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

NSIT,Jetalpur Example Programs Programs

NSIT,Jetalpur Echo.java Echo.java C:\UMBC\331\java>type echo.java // This is the Echo example from the Sun tutorial class echo { public static void main(String args[]) { for (int i=0; i < args.length; i++) { System.out.println( args[i] ); } } } C:\UMBC\331\java>javac echo.java C:\UMBC\331\java>java echo this is pretty silly this is pretty silly C:\UMBC\331\java>

NSIT,Jetalpur Factorial Example Factorial Example /* This program computes the factorial of a number */ */ public class Factorial { // Define a class public static void main(String[] args) { // The program starts here public static void main(String[] args) { // The program starts here int input = Integer.parseInt(args[0]); // Get the user's input int input = Integer.parseInt(args[0]); // Get the user's input double result = factorial(input); // Compute the factorial double result = factorial(input); // Compute the factorial System.out.println(result); // Print out the result System.out.println(result); // Print out the result } // The main() method ends here } // The main() method ends here public static double factorial(int x) { // This method computes x! public static double factorial(int x) { // This method computes x! if (x < 0) // Check for bad input if (x < 0) // Check for bad input return 0.0; // if bad, return 0 return 0.0; // if bad, return 0 double fact = 1.0; // Begin with an initial value double fact = 1.0; // Begin with an initial value while(x > 1) { // Loop until x equals while(x > 1) { // Loop until x equals fact = fact * x; // multiply by x each time fact = fact * x; // multiply by x each time x = x - 1; // and then decrement x x = x - 1; // and then decrement x } // Jump back to the star of loop } // Jump back to the star of loop return fact; // Return the result return fact; // Return the result } // factorial() ends here } // factorial() ends here } // The class ends here

NSIT,Jetalpur Constructors Constructors Classes should define one or more methods to create or construct instances of the class Classes should define one or more methods to create or construct instances of the class Their name is the same as the class name 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 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. 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!) Constructors automatically invoke the zero argument constructor of their superclass when they begin (note that this yields a recursive process!)

NSIT,Jetalpur Methods, arguments and return values Java methods are like C/C++ functions. Java methods are like C/C++ functions. General case: General case: returnType methodName ( arg1, arg2, … argN) returnType methodName ( arg1, arg2, … argN) { methodBody 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() {}