No Slide Title
Download
Report
Transcript No Slide Title
Java Fundamentals
MIS 3023
Business Programming Concepts II
The University of Tulsa
Professor: Akhilesh Bajaj
All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved
Objectives
• Understand the building blocks of programs
• Learn the basic Java types
TM
• Take another look at our first lab
Let’s get started!
Building Blocks of Programming
• Data & Instructions are the 2 fundamental building blocks.
• Data: Types & Variables
• Instructions: Control structures & subroutines
Data: A type is the set of possible values that a variable of that type
can contain.
E.g., what values can a variable of type int contain?
A variable is a label or a name for a main memory location.
Memory maps: Maps of the main memory of the process as it
executes, showing the data being used by the process.
Building Blocks of Programming
• Instructions: Control structures & subroutines
• Control structures manage the “flow of control”. The basic
structures are: branching (if, switch) and looping (for, while,
do-while).
• Subroutines are used to chunk tasks. We divide the overall task
our program needs to perform into chunks, and then label each chunk
with the name of the subroutine. In Java, subroutines are called
methods. In C/C++ they are called functions. In BASIC, they are called subroutines.
In Pascal, they are called procedures and functions.
• It’s very important to plan the program so that it is broken into
meaningful chunks. Rules of thumb for chunking:
a) If we are typing the same code again and again, we should write it
in a method, and then call the method by name in the program.
b) If it helps us solve the overall problem by assuming a certain part is
already taken care of in the method, without worrying about how, then that
certain part can be coded as a subroutine or method and called in the program.
Basic Types in Java
• Java is strongly typed.
Primitive Types: These are the building blocks for more complex
types.
Type
Bit
Values
Size
Standard
boolean
8
true , false
char
16
‘\u0000’ to ‘\uFFFF’
byte
8
-128 to + 127
short
16
-32,768 to +32,767
int
32
-2,147,483,648 to
+2,147,483,647
long
64
-9,223,372,036,854,775,808
to + (that -1)
float
32
-3.40292347E+38 to + that
IEEE 754
floating point
double
64
-1.79769313486231570E+308
to + that
IEEE 754
floating point
ISO Unicode
Char Set
Basic Types in Java
• The most common numerical types we will use are:
int for integers, double for decimal points
• For char constants, put a single character in single quotes.
The Unicode char set contains 65,536 characters, in other words all
chars in all written languages. 255 chars in this scheme are ASCII.
So we just put whatever character we want in single quotes, as a char
constant.
The following special character values can be used:
‘\b’
backspace
‘\t’
tab
‘\n’
linefeed
‘\r’
carriage return
\''
double quote (a single quote will give us a single quote)
\\
backslash
Note: ‘3’ is a char, 3 is a numeric literal, ‘a’ is a char, a is a
Basic Types in Java
• String: This is not a primitive type in Java. However, it is used so
often, that we need to look at it right away. A String is a class
in the standard Java library. A class is more complex than a type.
We create variables of types But we create objects of classes.
We create objects of class String, every time we need a String.
Each object we create has a name, data (the actual string
of characters) and certain operations (methods) that we can use on that
object.
String greeting = “Hello”;
String emptyStr = “”; //an empty string
System.out.println(greeting + ‘\n’);
Prints out Hello and then a new line.
List of useful methods for String class in our text book:
\\bahweb\nfp\bajaja\MIS3023\Text\javanotes4.1\c2\s3.html
Basic Types in Java
• String: We do not need to understand how these object methods work.
We use them as black boxes. A lot of the power of Java is the API
(Application Programming Interface) which consists of a lot of classes
that we can use, without really looking at the code that went into them.
www.java.sun.com lists the current API for Java.
Classes can have two types of methods in Java:
a) A class can have static methods that are called by
Classname.methodname(any parameters)
b) A class can have non-static methods that
need an object created first of that class, and then they can be used.
The String methods we saw are non-static. println() is a static
method called on class System.out without declaring an object
of that class.
Revisiting our First Lab
Import hints to Java compiler where to look for classes we are using
We write every file as a class. The file name we save it as MUST be the same as the class name
with a .java extension.
Each program has a main() method that is run first. In this example, that
is the ONLY method.
The println() method is a static method in the out class, which is a subclass
of the System class.
import java.io.*;
public class HelloWorld {
// A program to display the message
// "Hello World!" on standard output
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
// end of class HelloWorld
Fun In Class Assignment
Let us think about a car payment calculator program, to help a car
dealership’s customers decide how much they can pay.
• What variables (data) do we need to solve the problem?
• What flow (process) do we need to solve the problem?
Write a sketch of the program on paper.
CONCLUSION
• We took a look at the basic building blocks of
programming
•We learnt basic types and the String Class
Chapter 2, section 2 in our book has more explanation:
http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4.
1/c2/s2.html
• We began to understand classes and how they work