0% found this document useful (0 votes)
22 views36 pages

Java Programming Basics Explained

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views36 pages

Java Programming Basics Explained

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Lecture-03

Basics of Java Programming


By
Dr. Bharati Mishra
Agenda
∙ Evaluating Expressions
∙ Operator Precedence
∙ Case Study: Displaying the Current Time,
∙ Augmented Assignment Operators

∙ Increment and Decrement Operators

∙ Numeric Type Conversions


Arithmetic Expression to Java Expression
(3 + 4 * x) / 5 – 10 * (y - 5) * (a + b + c) / x +
9 * (4 / x + (9 + x) / y)
Arithmetic Expressions

• Operators have precedence


1. Parentheses ()
2. *, /, %
3. +,-
4. …
Shortcut Operators

• Shortcut operators for assignment

Operator Example Equivalent


+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
++/--

• Increment, decrement operators


Operator Name Description

++var pre-increment The expression (++var) increments var by 1 and evaluates


to the new value in var after the increment.
var++ post-increment The expression (var++) evaluates to the original value
in var and increments var by 1.
--var pre-decrement The expression (--var) decrements var by 1 and evaluates
to the new value in var after the decrement.
var-- post-decrement The expression (var--) evaluates to the original value
in var and decrements var by 1.
++/--

• Increment, decrement operators


Conversions

• Example
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

• When having different types of operands


• Operands will be automatically converted to the
largest operand in the expression
Conversions

• Implicit casting (type widening)


int x = 3; double d = x;
• A small number fits easily in a large variable
• Explicit casting (type narrowing)
double d = 3.9;

int i = (int)d;

• 3.9 cannot be fit in an int, so fraction part is


truncated.
• Overflow: value too large to be stored

byte b = 300;
Tracing a Simple Program
Trace of Program
allocate memory for
public class ComputeArea { radius.
(it is a “variable”)
/** Main method */
public static void main(String[] args) {
double radius; radius no value
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
Trace of Program

public class ComputeArea {


/** Main method */
public static void main(String[] args) {
double radius; radius no value
double area; area no value

// Assign a radius
radius = 20; Allocate memory for
area.
// Compute area (It is a “variable”).
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
Trace of Program

public class ComputeArea { Assign value to


radius
/** Main method */
public static void main(String[] args) {
double radius; radius 20
double area; area no value

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
Trace of Program

public class ComputeArea {


/** Main method */
public static void main(String[] args) {
double radius; radius 20
double area; area 1256.636

// Assign a radius
radius = 20; Compute area and
assign it to the
variable
// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
Other Data Types in Detail ..
Character Type

• char ch = ‘a’;
• ASCII
• Initial encoding
• Unicode
• More recent encoding, allowing international
encoding
• A 16-bit encoding scheme, preceded by \u,
expressed in four hexadecimal numbers that run
from '\u0000' to '\uFFFF'.
• char ch = ‘\u0003’;
Character Type

• How to declare?
• char letter = 'A'; (ASCII)
• char numChar = '4'; (ASCII)
• char letter = '\u0041'; (Unicode)
• char numChar = '\u0034'; (Unicode)
• Representing a numerical code (e.g. ‘a’=97)
• You can use +/- on char

char ch = 'a';
System.out.println(++ch);
Character Type

• Java characters use Unicode


• Supports display of written texts in the world’s
diverse languages.

Unicode \u03b1 \u03b2 \u03b3


for three Greek letters
Escape Characters

• Special characters called “escape” characters


Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022
Escape Characters

• Example 1
• System.out.println(“Java is fun!”);
• Output: Java is fun!
• Example 2
• System.out.println(“Java is \n fun!”);
• Output: Java is
fun!
• Equivalent to:
• System.out.println(“Java is”);
• System.out.println(“fun!”);
String Type

• Char type is one character


• E.g. ‘a’
• String type is sequence of characters
• String x = “Java is fun”;
• Not a primitive type!
• A predefined class in Java
• Reference type (more later in Chapter 7)
String Type

• Concatenate strings
1. String message = "Welcome " + "to " + "Java";
• //message becomes “Welcome to Java”
2. String s1 = "Chapter" + 2;
• // s1 becomes Chapter2
3. String s2 = "Supplement" + 'B';
• // s2 becomes SupplementB
Converting Strings

• Equivalent class for each data type


• Byte, Short, Integer, Long, Float, Double, Character
• Use the following method to convert from String
int x = Integer.parseInt(“85”);

• Use the following method to convert from String


double y=Double.parseDouble(“85.7”);
Programming Style
Programming Style

• Appropriate Comments
• Naming Conventions
• Proper Indentation and Spacing Lines
• Block Styles
Programming Style

• Comment is important
• Documenting your program
• Appropriate Comments
• Include a summary at the beginning of the program
• what the program does
• its key features
• its supporting data structures
• For this class:
• your name, class section, date, and a brief description at the
beginning of the program.
Programming Style

• Choose meaningful and descriptive names, not


cryptic names!
• Variables and method names
• Use lowercase.
• If the name consists of several words, capitalize the
first letter of each subsequent word in the name.
• computeArea()
• Class names
• Capitalize the first letter of each word in the name
• BankAccount
• Constants
• Capitalize all letters in constants, use underscores
• MAX_VALUE
Programming Style

• Indentation
• Blocking styles
• Spacing
• Use blank line to separate segments of the code.

Indentation
Programming Errors
Programming Errors

• Syntax Errors
• Detected by the compiler
• E.g. “iNT x;” instead of “int x;”
• Runtime Errors
• Causes the program to abort
• E.g. x= 3/0;
• Logic Errors
• Produces incorrect result
• Tax = 1.0 * income;
Syntax Errors

• Syntax Errors

public class ShowSyntaxErrors {


public static void main(String[] args) {
i = 30;
System.out.println(i + 4);
}
}
Runtime Errors

• Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
}
}
Logical Errors

• Logical Errors
• Error in design of program
• E.g. using the wrong formula for computing the area of
a circle
Debugging

• Logical Errors are called “bugs”


• The process of finding and correcting errors is
called debugging.
• narrow down to where the bug is located
1. hand-trace the program (read your program)
• Difficult
2. insert print statements to show the values of the
variables
• Still not very effective
3. Use a debugger utility
Debugging

• History of term “bug”

Admiral Grace
Hopper
Debugging

• Debugger: the program for debugging


• Usually part of IDE
• It allows you to
• Execute a single statement at a time
• Trace into or stepping over a method
• Set breakpoints
• Display variables

You might also like