INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

Object Oriented Programming in JAVA
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
CMT Programming Software Applications
Outline Java program structure Basic program elements
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Hello, world! Dissect HelloWorld.java Compile it Run it.
String Escape Sequences
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
The Java Programming Language
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
CT1513 Introduction To java © A.AlOsaimi.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Working with Java.
Chapter 6 GC 101 Strings.
Introduction to.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Data types and variables
Programming Language Concepts (CIS 635)
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
2.1 Parts of a C++ Program.
Java Fundamentals 3 Type conversion ,String.
Chapter 2: Basic Elements of Java
Introduction CSC 111.
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
elementary programming
Chapter 2: Java Fundamentals
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
Chap 2. Identifiers, Keywords, and Types
Java Fundamentals 3 Type conversion ,String.
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Presentation transcript:

INTRODUCTION TO JAVA CHAPTER 1 1

WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The language derives much of its syntax from C and C++ but has a simpler object model and fewer low- level facilities. The Java language is accompanied by a library of extra software that we can use when developing programs. The library provides the ability to create graphics, communicate over networks, and interact with databases. The set of supporting libraries is huge. 2

JAVA APPLICATIONS AND APPLETS 3 Applications – standalone Java programs Applets – Java programs that run inside web browsers Java is the first programming language to deliberately embrace the concept of writing programs that. can be executed on the Web.

COMPILING JAVA PROGRAMS  The Java compiler produces bytecode (a “.class” file) not machine code from the source code (the “.java” file).  Bytecode is converted into machine code using a Java Interpreter 4

PLATFORM INDEPENDENT JAVA PROGRAMS COMPILING  You can run bytecode on any computer that has a Java Interpreter installed 5 “Hello.java”“Hello.class”

FUNDAMENTALS 6

HELLO WORLD JAVA PROGRAM HELLO WORLD JAVA PROGRAM // import section public class MyFirstprogram { // main method public static void main( String args[] ){ System.out.println(“Hello World”); } // end main } // end class 7

SAVING, COMPILING AND RUNNING JAVA PROGRAMS  Saving a Java program :  A file having a name same as the class name should be used to save the program. The extension of this file is ”.java”. “MyFirstprogram.java ”.  Compiling a Java program : Call the Java compiler javac The Java compiler generates a file called” MyFirstprogram.class” (the bytecode).  Running a Java program  Call the Java Virtual Machine java: java MyFirstprogram.class 8

COMMENTS IN A JAVA PROGRAM Comments are used to describe what your code does its improve the code readability. The Java compiler ignores them. Comments are made using  // which comments to the end of the line,  /* */, everything inside of it is considered a comment (including multiple lines). Examples: /* This comment begins at this line. This line is included in this comment It ends at this line. */ // This comment starts here and ends at the end of this line. 9

GOOD PROGRAMMING PRACTICE Be careful java is a sensitive language. It is good to begin every program with a comment that states the purpose of the program and the author. Every java program consist of at least one class that the programmer define. Begin each class name with capital letter. The class name doesn’t begin with a digit and doesn’t contain a space. Use blank lines and spaces to enhance program readability When you type an opining left brace{ immediately type the closing right brace} Indent the entire body of each class declaration 10

PROGRAMS AND DATA  Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory.  A space memory has three characteristics Identifier Data Type State 11

IDENTIFIER 12 is a sequence of characters that denotes the name of the space memory to be used. This name is unique within a program. Identifier Rules It cannot begin with a digit (0 – 9). It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed. Identifiers in Java are case-sensitive. The identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

STATE My be changed  variable All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word. Cannot be changed  constant All uppercase, separating words within a multiword identifier with the underscore symbol, _. 13

DATA TYPE The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program. 14

JAVA BUILT-IN DATA TYPES 15

PRIMITIVE DATA TYPES 16 Type Size (bits) RangeDescription booleantrue, falseStores a value that is either true or false. char160 to 65535Stores a single 16-bit Unicode character. byte8-128 to +127Stores an integer. short to Stores an integer. int32 bits-2,147,483,648 to +2,147,483,647 Stores an integer. long64 bits-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Stores an integer. float32 bitsaccurate to 8 significant digitsStores a single-precision floating point number. double64 bitsaccurate to 16 significant digitsStores a double-precision floating point number.

VARIABLE/CONSTANT DECLARATION  When the declaration is made, memory space is allocated to store the values of the declared variable or constant.  The declaration of a variable means allocating a space memory which state (value) may change.  The declaration of a constant means allocating a space memory which state (value) cannot change. 17

CONSTANT DECLARATION final dataType constIdentifier = literal | expression; final double PI = ; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; 18 These are called literals. This is called expression.

VARIABLE DECLARATION  A variable may be declared:  With initial value.  Without initial value  Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3;  Variable declaration without initial value; dataType variableIdentifier; double avg; int i; 19

MORE DECLARATION EXAMPLES  String declaration  with initial value:  String word="Apple";  without initial value:  String word;  char declaration  with initial value:  char symbol ='*';  without initial value:  char symbol; 20  Boolean declaration:  with initial value:  boolean flag=false;  boolean valid=true;  without initial value:  boolean flag;

VARIABLES  Float  The default type of floating point numbers is double.  The declaration : float rate = 15.5f ; without the f, the compiler will generate an error 21

VARIABLES  Char  When using the char data type, you enclose each character represented within single quotations marks.  Ex: char c = ‘A’ char space = ‘ ‘  Each character is represented by a value (‘A’ is represented by the value 65) 22 char aCharecter='A'; char aAscii =65; System.out.println(aCharecter); System.out.println(aAscii); AAAA

23

TYPE CONVERSION (CASTING)  Used: to change one data type to another. to avoid implicit type correction.  Syntax: (dataTypeName) expression  Expression evaluated first, then the value is converted to dataTypeName 24

TYPE CONVERSION (CASTING)  Examples: 1. (int)( ) = (int)(7.9) + (int)(6.7) = (double)(17) = (double)(7) /2 = 7.0/2 = (double)(7/2) = (int)(7.8+(double)(15)/2) =(int)15.3 =15 25 double x=7.9,y= 6.7; int result; result=(int)( );

TYPE CONVERSION (CASTING) 8. (int)(‘A’) = (int)(‘8’) = (char)(65) = ‘A’ 11. (char) (56) = ‘8’ 26

THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed in double quotation marks.  Is processed as a single unit.  Null or empty strings have no characters. “ “  Every character has a relative position, the first character is in position 0. 27

Java Programming: From Problem Analysis to Program Design, Second Edition28 THE CLASS STRING  Java system automatically makes the class String available (i.e no need to import this class )  Example : Consider the following declaration : String sentence ; sentence = “programming with java” 28

THE CLASS STRING  Length of the string is the number of characters in it.  When determining the length of a string, blanks count.  Example :  “ “  has length = 0  “abc”  has length = 3, position of a = 0,b= 1, c= 2  “a boy”  has length = 5 29

Java Programming: From Problem Analysis to Program Design, Second Edition30 SOME COMMONLY USED STRING METHODS String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.charAt(3)); System.out.println(mystr.indexOf('J')); System.out.println(mystr.indexOf(‘j')); programming with Java is fun g 17 30

Java Programming: From Problem Analysis to Program Design, Second Edition31 SOME COMMONLY USED STRING METHODS 31

EXAMPLE String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.indexOf('a',10)); System.out.println(mystr.indexOf("with")); System.out.println(mystr.indexOf("ing")); programming with Java is fun

Java Programming: From Problem Analysis to Program Design, Second Edition33 SOME COMMONLY USED STRING METHODS 33

Java Programming: From Problem Analysis to Program Design, Second Edition34 EXAMPLES ON STRING METHODS String s1, s2, s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG 34

Java Programming: From Problem Analysis to Program Design, Second Edition35 EXAMPLES ON STRING METHODS System.out.println(s1.substring(1, 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6 35