Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
Java Syntax Primitive data types Operators Control statements.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
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™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
August 6, 2009 Data Types, Variables, and Arrays.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
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?
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Java Data Types.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Java Programming Language Lecture27- An Introduction.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
JAVA MULTIPLE CHOICE QUESTION.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Yanal Alahmad Java Workshop Yanal Alahmad
Multiple variables can be created in one declaration
An Introduction to Java – Part I
Chapter 2.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Chap 2. Identifiers, Keywords, and Types
Chapter 3 Introduction to Classes, Objects Methods and Strings
Review of Java Fundamentals
Presentation transcript:

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA

1. LANGUAGE FUNDAMENTALS  Source Files  Keywords and Identifiers  Primitive Data Types  Literals  Arrays  Class Fundamentals  Argument Passing  Garbage collection

Source Files  “.java” extension  At most one top-level public class definition (unlimited number of non-public class definitions)  Class name= unextended filename  Three top-level elements (None of them required) 1.Package declaration (package) 1.import statements 2.Class definitions

Test.java // Package declaration package exam.prepguide; // Imports import java.awt.Button; // a class import java.util.*; // a package // Class Definition public class Test {... }

Keywords and Identifiers  Keywords and reserved words may not be used as identifiers!!  An identifier must begin with a letter, a dollar sign (‘$’), or an underscore (‘_’); subsequent characters may be letters, ‘$’, ‘_’, or digits Identifier : word used by a programmer to name a word used by a programmer to name a variable, method, class, or label

Primitive Data Types  Boolean variables : true or false  4 signed integral data types: byte, short, int and long TypeRepresentation Minimum Maximum TypeRepresentation Minimum Maximum (bits) (bits) boolean1 boolean1 char char byte byte short short int int long long float32 float32 double64 double64

Char: Char: 16-bits encoding Floating-point types: floatdouble float and double IEEE 754 specification t t[0]t[1]t[2]t[3]t[4] Float.NanFloat.NEGATIVE_INFINITYFloat.POSITIVE_INFINITYDouble.NanDouble.NEGATIVE_INFINITYDouble.POSITIVE_INFINITY Nan: Not a number 1. double d=-10.0/0.0; 2. if (d==Double.NEGATIVE_INFINITY) { 3.System.out.println(“d just exploded”+d); 4. }

Literals boolean: boolean isBig=true; boolean isLittle=true; A value that may be assigned to a primitive or string variable or passed as an argument to a method call t[0]t[1]t[2]t[3]t[4] char: char c=´w´; char c= ´\u4567´; Unicode hexadecimal

´\n’new line ´\r’return ´\t’tab ´\b’backspace ´\f’formfeed ´\’ ’single quote ´\” ’double quote ´\\ ’backslash SPECIAL CHARACTERS

Literals... integral (octal) 0x1c0X1c 0x1C floating point Decimal point : Decimal point : Letter E or e (scientific notation): Letter E or e (scientific notation): 4.23E E+21 Suffix f or F (float 32 bits) Suffix f or F (float 32 bits)1.828f Suffix D or d (double 64 bits): Suffix D or d (double 64 bits):1234d (default: double 64 bits) String: String s =“Characters in strings are 16 bits”;

Arrays... t[0]t[1]t[2]t[3]t[4] An ordered collection of primitives, object references, or other arrays Homogeneous: elements of the same type Homogeneous: elements of the same type 1.Declaration: name and type of its elements 2.Construction 3.Initialization

Array declaration int ints[]; double dubs[]; Dimension dims[]; float toDee[] []; [] can come before or after the array name [] can come before or after the array name myMethod(double dubs []) myMethod(double[] dubs) double[] anotherMethod() double anotherMethod() []

Arrays... The declaration DOES NOT specify the size of an array!! Array size is specified at runtime via the new keyword The declaration DOES NOT specify the size of an array!! Array size is specified at runtime via the new keyword 1.int ints[]; // Declaration to the compiler 2.ints=new int[25]; // Run time construction 1.int size=1152*900; 2.int raster[]; 3.rater=new int[size]; Size can be specified with a VARIABLE

1.int ints[]=new int[25]; Declaration and construction in a single line automatic initialization when an array is constructed

ElementInitial TypeValue booleanfalse booleanfalse char‘ \ u0000’ char‘ \ u0000’ byte0 byte0 short0 short0 int0 int0 long0L long0L float0.0f float0.0f double0.0d double0.0d object reference null object reference null Automatic Initialization

1.float diameters[]={1.1f, 2.2f, 3.3f, 4.4f, 5.5f}; Initialization 1.long squares[]; 2.squares =new long[6000]; 3.for (int i=0; i<6000; i++) 4. squares[i]=i*i;

Class Fundamentals main() method  Entry point for JAVA applications  Application: class that includes main()  Execution: java at the command line, followed by the name of the class public static void main(String args[]) % java Mapper France Belgium Command line args[1]=“France”args[2]=“Belgium”

Variables and initialization  Member Variable:  When an instance is created  Accessible from any method in the class  Automatic (Local) Variable:  On entry to the method  Exists only during execution of the method  Only accessible within the method 1.class HasVariables { 2.int x=20; 3.static int y =30;

public int wrong() { int i; int i; return i+5; return i+5;} public double fourthRoot(double d) { double result; double result; if (d>=0) { if (d>=0) { result=Math.sqrt(Math.sqrt(d)); result=Math.sqrt(Math.sqrt(d)); } return result; return result;} Error: “variable i may not have been initialized” Error: “variable result may not have been initialized”

public double fourthRoot(double d) { double result=0.0; // initialize double result=0.0; // initialize if (d>=0) { if (d>=0) { result=Math.sqrt(Math.sqrt(d)); result=Math.sqrt(Math.sqrt(d)); } return result; return result;} Correct Initialization!!

Argument passing A copy of the argument that gets passed double radians=1.2345; System.out.println(“Sine of”+radians +”=“+Math.sin(radians)); +”=“+Math.sin(radians)); Changes to the argument value DO NOT affect the original data

Argument passing... public void bumper(int bumpMe) { bumpMe+=15; bumpMe+=15;} int xx=12345; bumper(xx); System.out.println(“Now xx is”+xx);

Object Reference Button btn; Btn=new Button(“Ok”); When an object is created, a value (bit pattern) that identifies the object is returned

Button btn; btn = new Button(“Good”); replacer(btn);System.out.println(btn.getLabel()); public void replacer(Button replaceMe) { replaceMe=new Button(“Evil”); replaceMe=new Button(“Evil”);} The string printed out is ”Good”

TextField tf; tf = new TextField(“Yin”); changer(tf);System.out.println(tf.getText()); public void changer(TextField changeMe) { changeMe.setText(“Yang”); changeMe.setText(“Yang”);} The string printed out is ”Yang”, not ”Yin”

How to create a Reference to a primitive public class PrimitiveReference { public class PrimitiveReference { public static void main( String args []) public static void main( String args []) int [] myValue ={ 1 }; int [] myValue ={ 1 }; modifyIt(myValue); modifyIt(myValue); System.out.println(“myValue contains “+ System.out.println(“myValue contains “+ myValue[0]); myValue[0]); } public static void modifyIt(int [] value) { public static void modifyIt(int [] value) { value [0]++; value [0]++; } }

Garbage Collection Storage can remain allocated longer than the lifetime of any method call releasing the storage when you have finished with it:  Corrupted data: releasing storage too soon  Memory Shortage: forget to release it

Automatic Garbage Collection: Automatic Garbage Collection: The runtime system keeps track of the memory that is allocated and determines whether it is still usable Garbage collector: Low-priority thread

public Object pop() { public Object pop() { return storage[index--]; return storage[index--];} public Object pop() { public Object pop() { Object returnValue=storage [index]; Object returnValue=storage [index]; storage[index--] =null; storage[index--] =null; return returnValue; return returnValue;}