Basic Java Chapter 2

  • Uploaded by: zyrus-mojica-6477
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Basic Java Chapter 2 as PDF for free.

More details

  • Words: 1,409
  • Pages: 19
Add Comments to a Java Program

To all programs, comments must always be present, be it short or long. It is very essential to include comments that are non-executable statements for documentation purposes. It helps large programs to be read easily and to determine why the function or attributes etc. are needed in the program. It is also reliable for future references.

Three comments in Java  // ←end-of-line or single comment  /* */ ← multiple line comment  /** */ ← javadoc comment

Save, Compile and Execute a Java Programming After creating a Java programming, the Java source code has to be saved with an extention .java e.g. HelloWorld.java. It is important that the filename and the classname must be the same. All compilers impose case sensitive in Java. 2 steps to perform before viewing the program output: The source code written must be compiled into bytecode The java interpreter will then translate the bytecode into executable statements. How to compile a Java application To compile HelloWorld.java do: javac HelloWorld.java To run the application do: java HelloWorld

Modify a Java Program To edit the source code you may open up your existing Java source code using any text editor.

Before the new source can be executed, the following must be performed: 1. The file with changes must be saved using the same filename 2. The program must be recompiled again with the javac command 3. The bytecode must then be interpreted by the Java interpreter, using the java command, to execute the program

Java Programming Conventions Three comments in java // ←end-of-line or single comment /* */ ← multiple line comment /** */ ← javadoc comment A statement is always terminated by a semicolon (;) A block is a collection of statements bounded by opening and closing braces, it can also be used in a class definition. A block can also be nested Any amount of whitespace is allowed in a Java program Classes - names should be nouns, in mixed case, with the first letters of each word capitalized. Interfaces – names should be capitalized Methods – names should be verbs, in mixed case, with the first letter in lowercase Variables – should be in mixed case with a lowercase first letter Constants – should be all uppercase in mixed case with a lowercase first letter Control structures – use braces { }

Identifiers and Keywords Identifiers are names given to a variable, class or method You can use letters, digits, dollar signs, but the first letter of the Identifier must always start using a letter, underscore (_) or a dollar sign. Java identifiers are case sensitive it treats upper and lower case different characters. You cannot use a space or other symbols in an identifier. Java identifiers doesn’t have maximum length. Keywords must not be used as names of a variable, class or method. There are 48 reserved keywords: abstract do

implements

private

throw

char

finally

boolean double

import

protected

throws

native

super

break

else

instanceof

public

transient volatile class

byte

extends int

return

true

float

new

case

false

interface

short

try

switch

while

catch

final

long

static

void

continue for

Constants and Variables There are 2 types of identifiers in Java programming: constant and variables. Constant are data or values stored that do not change during the execution of the program. An identifier can be a symbolic constant, which values are unalterable. To prevent alteration, when creating a symbolic constant the keyword “final” is inserted before the variable name. A constant is always initialized with a value where this value cannot be changed after it is declared. syntax: static final type identifier = value; Variables are data or values stored that can be changed during the execution of the program. Again it is always initialized before being used. How to declare a variable A data type that identifies the type of data that the variable will store An identifier that is variable’s name An optional assigned value, if we want a variable to contain a value syntax: type identifer = value; or type identifier1, identifier2;

Data Types There are 2 categories of data types in Java programming, which are primitive and reference types.

Eight Primitive types of data: boolean long

byte

short

char

float

int

double

Java programming language provides for two complex data types under the reference type. array class

Integer There are four variations of data types classified under the category of integer: byte

short

int

long

*when defining a variable as long, a letter ‘L’ or ‘l’ must be attached behind the value. Type

Minimum range

Maximum range

Size

Byte

-128

127

8 bits

Short

-32,768

32,767

16 bits

Int

-2,147,483,648

2,147,483,647

32 bits

*long

9,223,372,036,854,775,80764 bits 9,223,372,036,854,775,808

Floating point A floating point number contains decimal positions. The Java programming language supports two floating point data types: float and double. *when defining a variable explicitly as float, a letter ‘F’ or ‘f’ must be attached behind the value.

Type

Minimum range

Maximum range

Size

*float

-1.7e-308

1.7e+308

32 bits

double

3.4e-038

3.4e+038

64 bits

Character There is only one data type under the category of character, which is char data type. The char data type can only hold any single character. The value assigned to a char must be enclosed within single quotation marks: ‘ ‘ Example: char num = ‘8’; Array An array is a list of variables, which all have the same data types and same name. An array can be declared by inserting a pair of square brackets after the type. syntax: type array_name[]; or type[] array_name; Class String is a class in java programming. Each created string is a class object. A String variable name is actually refers to a location in memory rather than to a particular value. Examples: String myWord = “Java is not easy”;

Separators Separators are used to define the shape and function of Java code. Separator

Function

{}

To separate blocks of code

;

To delimit the end of a line code

,

To separate a list of variables or values

[]

To define and reference arrays

()

To indicate precedence in an expression e.g. (x*y) + z

.

To separate a class from a subclass e.g. System.out

Types of Operators

 arithmetic operator  relational or comparison operator  logical operator  assignment operator  increment/decrement operator

Arithmetic Operator There are five standard arithmetic operators, which can be used to manipulate values in the programs. These operators are used in mathematical expression.

Operator

Desciption

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus

Relational or Comparison Operator There are six comparison operators used in Java programming. A comparison operator allows us to compare two items.

Operator

Desciption

<

Less than

>

Greater than

==

Equal to

<=

Less than or equal to

>=

Greater than or equal to

!=

Not equal to

Logical Operator There are three logical operators used in java programming. It is used in some cases to combine compound conditions.

Operator

Description

||

OR

&&

AND

!

NOT

Increment or Decrement Operator

When we need to increate the value of the variable by exactly one we can use the 2 operators, that is, pre-increment and post-increment. We can use the syntax below to increment a certain value of a variable. example of pre-increment: ++identifier example of post-increment: identifier++ We can also decrease a variable’s value by exactly one by using the 2 operator pre-decrement and post-decrement. We can use the syntax below to decrement a certain value of a variable. example of pre-decrement: --identifier; example of post-increment identifier--;

Escape Sequences In Java Programming language, we use an escape to store any characters that includes non-printing characters such as a backspace or a tab that is in a char variable. Escape Sequence

Description

\b

Backspace

\t

Tab

\n

Newline or linefeed

\f

Form feed

\r

Carriage return

\”

Double quotation mark

\’

Single quotation mark

\\

Backslash

Programming Exercises

1. State the different types of comments used in Java programming and how it should be used. 2. Give the syntax for the following: a. Save a Java source code b. Compile a Java program c. Execute a Java program 3. Explain the difference between a constant and a variable 4. State the different types of data types used in Java programming 5. List the different types of operators used in Java programming 6. Are the following statements legal: char c = ‘a’; int i = c + 10;

Related Documents

Basic Java Chapter 2
June 2020 3
Basic Java Chapter 3
June 2020 4
Basic Java Chapter 1
June 2020 2
Basic Java Chapter 4
June 2020 5
Java Basic Book 2
October 2019 24
Basic Java
November 2019 17