1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.

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
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.
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.
Some basic I/O.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
CSC3170 Introduction to Database Systems
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
DAT602 Database Application Development Lecture 5 JAVA Review.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSE 131 Computer Science 1 Module 1: (basics of Java)
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
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.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
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.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
OOP Basics Classes & Methods (c) IDMS/SQL News
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,
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Eclipse.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Internet and Java Foundations, Programming and Practice
Introduction to Programming in Java
Statements, Comments & Simple Arithmetic
Starting JavaProgramming
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Chapter 1: Computer Systems
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Anatomy of a Java Program
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C

2 Contents Hello World Program Statements Explained Java Program Structure in General Java Classes and Static Methods Data Types, Variables and Constants Java Comments and Documents Control Flow Reading from Keyboard Command Line Arguments Processing Summary and References

3 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(String args[]) { System.out.println(“Hello World”); }

4 Hello World: Java and C // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(String args[]) { System.out.println(“Hello World”); } /* helloworld.c: Hello World program */ #define void main(int argc, char *argv[]) { printf(“Hello World\n”); } S1: S2: S3: S4: S6:

5 Program Processing Compilation # javac HelloWorld.java results in HelloWorld.class Execution # java HelloWorld Hello World

6 Closer Look at - Hello World The class has one method – main() public static void main(String args[]) { System.out.println(“Hello World”); } Command line input arguments are passed in the String array args[] e.g java HelloWorld John Jane args[0] – John args[1] – Jane

7 Closer Look at - Hello World import java.lang.*; Java allows grouping of related classes into a package. It allows different companies can develop different packages, may even have same class and method names, but they differ by package name. Think of this package as library. “import” statement somewhat serves similar purpose as C’s #include

8 Java imports java.lang.* by default So, You don't need to import java.lang.* That means, you can invoke services of java’s “lang” package classes/entities, you don’t need to use fully qualified names. We used System.out.println() instead of java.lang.System.out.println()

9 public static void main (String args[]) public: The keyword “public” is an access specifier that declares the main method as unprotected. static: It says this method belongs to the entire class and NOT a part of any objects of class. The main must always be declared static since the interpreter users this before any objects are created. void: The type modifier that states that main does not return any value.

10 System.out.println(“Hello World”); java.lang.* All classes/items in “lang” package of java package. System is really the java.lang.System class. This class has a public static field called out which is an instance of the java.io.PrintStream class. So when we write System.out.println(), we are really invoking the println() method of the “out” field of the java.lang.System class.

11 Java Program Structure Documentation Section Package Statement Import Statements Interface Statements Class Declarations Main Method Class { }

12 More Java: Classes and static methods // SquareRoot.java: compute square root of number import java.lang.Math; class SquareRoot { public static void main(String args []) { double x = 4; double y; y = Math.sqrt(x); System.out.println("y= "+y); }

13 Basic Data Types Types boolean either true or false char16 bit Unicode 1.1 byte8-bit integer (signed) short16-bit integer (signed) int32-bit integer (signed) long64-bit integer (singed) float32-bit floating point (IEEE ) double64-bit floating point (IEEE ) String (class for manipulating strings) Java uses Unicode to represent characters internally

14 Variables Local Variables are declared within the block of code Variable has a type preceding the name Initial value is set by initialization expressions. type variableName = initialValue; e.g. int x = 1; Variables can be defined just before their usage (unlike C) e.g., for( int i = 0; i < 10; i++)

15 Constants Constants are similar to variables except that they hold a fixed value. They are also called “READ” only variables. Constants are declared with the reserved word “final”. final int MAX_LENGTH = 420; final double PI = ; By convention upper case letters are used for defining constants.

16 Declaring Constants - example class CircleArea { public static void main(String args[]) { final double PI = ; double radius = 5.5; // in cms double area; area = PI * radius * radius; System.out.println("Circle Radius = "+radius+" Area="+area); }

17 Comments English text scattered through the code are comments JAVA supports 3 types of comments /* */ - Usually used from multi-line comments // - Used for single line comments /** */ - Documentation comments

18 Javadoc Effort to make Java self-documenting True OOP style, encapsulate documentation within code :) Comments beginning with /** and ending with */ can be extracted and turned into html documentation

19 Control Flow Control Flow Statements in JAVA while loop for loop do-while loop if-else statement switch statement JAVA does not support a goto statement

20 while loop while (squared <= MAX) { squared = lo * lo; // Calculate square System.out.println(squared); lo = lo + 1; /* Compute the new lo value */ } Control Flow - Examples

21 Control Flow - Examples for loop for (int i = 1; i < MAX; i++) { System.out.println(i); // prints … }

22 do-while loop do { squared = lo * lo; // Calculate square System.out.println(squared); lo = lo + 1; /* Compute the new lo value */ } while (squared <= MAX); Control Flow - Examples

23 if-else loop if ( i < 10) { System.out.println(“i is less than 10” ); } else { System.out.println(“i is greater than or equal to 10”); } Control Flow - Examples

24 Control Flow - Examples switch statement switch (c) { case ‘a’: System.out.println (“ The character is ‘a’” ); break; case ‘b’; System.out.println (“ The character is ‘b’” ); break; default; System.out.println (“ The character is not ‘a’ or ‘b’” ); break; }

25 Summary We discussed meaning of statements in “hello world” program We discussed various basic constructs and syntax. Apart from OO specific items, most keywords or constructs in Java have similar meaning and usage style as C.