Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
Road Map Introduction to object oriented programming. Classes
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Java Syntax Primitive data types Operators Control statements.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Java Unit 9: Arrays Declaring and Processing Arrays.
Multiple Choice Solutions True/False a c b e d   T F.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
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.
CIT 590 Intro to Programming First lecture on Java.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
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 Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Working With Objects Tonga Institute of Higher Education.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Classes, Interfaces and Packages
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Object Oriented Programming Lecture 2: BallWorld.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Static and non-Static Chapter 5.
Unit-2 Objects and Classes
Defining Classes and Methods
Chapter 10 Thinking in Objects
Introduction to Java Programming
Built-In (a.k.a. Native) Types in C++
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Programs and Classes A program is made up from classes
Object Oriented Programming in java
Java Programming Language
Presentation transcript:

Moving from C to Java

The Java Syntax We Know

Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long  Floating Point: float, double And we even get a boolean type

Java Types There are no pointers in Java, but they aren’t necessary for referential types. All primitive types, i.e. the ones we know about are pass by value. All classes, wrappers, interfaces and arrays are actually references (Will go back to classes and arrays, leaver wrappers and interfaces for later)

The basics Everything in life has 3 important parts (not to say the other parts aren’t)  A state; The things that define this object from a similar one  An interface to the state; The ways we can interact with the state of this object Interfaces can be both public and private  A set of Behaviours; The things this object can do Java classes have all of these things too

The Skeleton Java Class Below is a basic Java Class Public class Name { /* State Goes Here*/ public Name(arguments){} //The constructor /*Methods go here*/ public static void main(String [] args){} } All Java classes must have a constructor They do not all have to have a main, only your main (Duhh!!!)

State in Java The state of a class in Java are the variables global to the class. Why?? This is because a class encapsulates an object, meaning it contains everything that is an object. State can of course be variables of any type

Variables of Class Type All class variables are referential types (think pointers) They all have a state, behaviours and a public interface detailing them. A giant library of classes in Java can be found in the API (bookmark this)

Java Arrays Earlier I said Arrays are referential types, what does this mean?? In Java when we say int numbers [] this is similar to int *numbers. We create a reference/pointer to a piece in memory but there is nothing there. Classes work the same way; String name is similar to char *name, it is only pointing to memory.

Instantiating And “new” Both classes and arrays must be instantiated to be used. We do this with the command new, which calls the constructor for us String name = new String (“Jeff”) In Java we have method overloading, which means we can have many functions with the same name but different arguments, like constructors char letters [] = {'a','j','e','f','f','z'}; String name = new String(letters,1,4);

Interface in Java When we say public java class, we are defining interface. Here are two main types of interfacing Java allows (more will be explained later)  Private; This means it is visible only within the class. State is usually private.  Public; This means that when you see the class you see this too. Constructors are public.

Behaviours in Java All the methods of a class are its behaviours. In C a function in only a behaviour because we define it to be, in Java a function is only not a behaviour when we define it that way. Because methods are native to the class they can access all class objects

Methods Example public class IntHolder { private int myNum; public IntHolder(int i) {myNum=i;} //Below is a very basic selector public int getNum() { return myNum; } Then when we want to use this method we simply go IntHolder temp = new Intholder(5); temp.getNum()

Back To The Basics Everything in life has 3 important parts (not to say the other parts aren’t)  A state; The things that define this object from a similar one  An interface to the state; The ways we can interact with the state of this object Interfaces can be both public and private  A set of Behaviours; The things this object can do Java classes have all of these things too

The SE view on life Lets take the example of a Cat  State = things like colour, breed, state of hunger, cleanliness, etc  Public interface = We can see the colour, we can feed the cat, we can pet the cadet.  Private interface = Only the cat can interface with its thoughts, you or I cannot (We don’t even really KNOW the cat has thoughts)  A set of behaviours = The cat can eat, sleep, preen, terrorize mice, etc

Cat in C To define the state we create the type Typedef struct { char* name; char* breed; int hunger; }Cat; The public interface is the contents of the header file The private interface would be any internal functions To define the behaviour we create other functions void eat(Cat *chat, int foodAmount) { chat->hunger = (chat->hunger-foodAmount) hunger- foodAmount); }

Cat in Java public class Cat {//The state private String name; private String breed; private int hunger; public Cat(){} // This is a basic constructor //This behaviour public void eat(int foodAmount) { hunger = (hunger-foodAmount)<0?0:(hunger- foodAmount); } }

Classes, Modules &SE Design (Oh My) The most obvious of the qualities is ofcourse modularity Abstraction; By viewing computer objects like real life object we can achieve a higher level of abstraction Information Hiding; Through public and private interfaces.