Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.

Slides:



Advertisements
Similar presentations
1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Advertisements

Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Object Oriented Programming in JAVA
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Principles of Object-Oriented Software Development The language Java.
Outline Java program structure Basic program elements
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
Java Language and SW Dev’t
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
An Introduction to Software Development Using JAVA
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Introduction to Computer Systems and the Java Programming Language.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Introduction to Java Java Translation Program Structure
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java What, Why and How….
Chapter No. : 1 Introduction to Java.
Chapter 3 GC 101 Java Fundamentals.
Introduction to.
Yanal Alahmad Java Workshop Yanal Alahmad
Internet and Java Foundations, Programming and Practice
Data types and variables
Programming Language Concepts (CIS 635)
Statements, Comments & Simple Arithmetic
Chapter 1: Computer Systems
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chap 1. Getting Started Objectives
JAVA BASICS.
Presentation transcript:

Introduction to Java CS 331

Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java applets, which run within browsers e.g. Netscape Example programs

Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects and operations It’s more platform independent –this makes it great for Web programming It’s more secure It isn’t C++

Applets, Servlets and Applications An applet is designed to be embedded in a Web page, and run by a browser Applets run in a sandbox with numerous restrictions; for example, they can’t read files and then use the network A servlet is designed to be run by a web server An application is a conventional program

Building Standalone JAVA Programs Prepare the file foo.java using an editor Invoke the compiler: javac foo.java This creates foo.class Run the java interpreter: java foo

Java Virtual Machine The.class files generated by the compiler are not executable binaries –so Java combines compilation and interpretation Instead, they contain “byte-codes” to be executed by the Java Virtual Machine –other languages have done this, e.g. UCSD Pascal This approach provides platform independence, and greater security

HelloWorld (standalone) public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } Note that String is built in println is a member function for the System.out class

Comments are almost like C++ /* This kind of comment can span multiple lines */ // This kind is to the end of the line /** * This kind of comment is a special * ‘javadoc’ style comment */

Primitive data types are like C Main data types are int, double, boolean, char Also have byte, short, long, float boolean has values true and false Declarations look like C, for example, –double x, y; –int count = 0;

Expressions are like C Assignment statements mostly look like those in C; you can use =, +=, *= etc. Arithmetic uses the familiar + - * / % Java also has ++ and -- Java has boolean operators && || ! Java has comparisons = > Java does not have pointers or pointer arithmetic

Control statements are like C if (x < y) smaller = x; if (x < y){ smaller=x;sum += x;} else { smaller = y; sum += y; } while (x < y) { y = y - x; } do { y = y - x; } while (x < y) for (int i = 0; i < max; i++) sum += i; BUT: conditions must be boolean !

Control statements II Java also introduces the try statement, about which more later switch (n + 1) { case 0: m = n - 1; break; case 1: m = n + 1; case 3: m = m * n; break; default: m = -n; break; }

Java isn't C! In C, almost everything is in functions In Java, almost everything is in classes There is often only one class per file There must be only one public class per file The file name must be the same as the name of that public class, but with a.java extension

Java program layout A typical Java file looks like: import java.awt.*; import java.util.*; public class SomethingOrOther { // object definitions go here... } This must be in a file named SomethingOrOther.java !

What is a class? Early languages had only arrays –all elements had to be of the same type Then languages introduced structures (called records, or structs ) –allowed different data types to be grouped Then Abstract Data Types (ADTs) became popular –grouped operations along with the data

So, what is a class? A class consists of –a collection of fields, or variables, very much like the named fields of a struct –all the operations (called methods) that can be performed on those fields –can be instantiated A class describes objects and operations defined on those objects

Name conventions Java is case-sensitive; maxval, maxVal, and MaxVal are three different names Class names begin with a capital letter All other names begin with a lowercase letter Subsequent words are capitalized: theBigOne Underscores are not used in names These are very strong conventions!

The class hierarchy Classes are arranged in a hierarchy The root, or topmost, class is Object Every class but Object has at least one superclass A class may have subclasses Each class inherits all the fields and methods of its (possibly numerous) superclasses

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

Another example of a class class Driver extends Person { long driversLicenseNumber; Date expirationDate; }

Creating and using an object Person john; john = new Person ( ); john.name = "John Smith"; john.age = 37; Person mary = new Person ( ); mary.name = "Mary Brown"; mary.age = 33; mary.birthday ( );

An array is an object Person mary = new Person ( ); int myArray[ ] = new int[5]; –or: int myArray[ ] = {1, 4, 9, 16, 25}; String languages [ ] = {"Prolog", "Java"};