0% found this document useful (0 votes)
64 views

Basic Concepts: I. Introduction To Java Microsystems, and Currently Owned by Oracle

The document introduces several basic Java concepts including: 1) Java is a platform-independent, modern programming language used for various applications like Android and web apps. 2) Java supports single-line and multi-line comments to explain code. Documentation comments generate external documentation. 3) Variables store and assign data of different types like integers, doubles, strings, and booleans. 4) Java provides operators for math, increment/decrement, and assignment to manipulate variable values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Basic Concepts: I. Introduction To Java Microsystems, and Currently Owned by Oracle

The document introduces several basic Java concepts including: 1) Java is a platform-independent, modern programming language used for various applications like Android and web apps. 2) Java supports single-line and multi-line comments to explain code. Documentation comments generate external documentation. 3) Variables store and assign data of different types like integers, doubles, strings, and booleans. 4) Java provides operators for math, increment/decrement, and assignment to manipulate variable values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SH1801

Basic Concepts
I. Introduction to Java
• Java is a high level, modern programming language designed in the early 1990s by Sun
Microsystems, and currently owned by Oracle.
• Java is platform-independent, which means that you only need to write the program once to
be able to run it on a number of different platforms.
• Java is portable, robust, and dynamic, with the ability to fit the needs of virtually any type of
application.
• Java is used to develop applications for Google’s Android OS, various desktop applications,
(such as media players and antivirus programs), web applications (such as interactive content),
enterprise applications (i.e., banking), and many more.

II. Java Comments


A. Comments
• The purpose of including comments in your code is to explain what the code is doing.
• Java supports both single and multi-line comments. All characters that appear within a
comment are ignored by the Java compiler.
• A single-line comment starts with two forward slashes and continues until it reaches the
end of the line. For example:
// this is a single-line comment
x = 3; // a single-line comment after code

B. Multi-Line Comments
• Java also supports comments that span multiple lines. For example:
/* This is also a
comment spanning
multiple lines */
• Note that Java does not support nested multi-line comments. However, you can nest single-
line comments within multi-line comments.
/* This is a single-line comment:

// a single-line comment
present
*/
C. Documentation Comments
• Documentation comments are special comments that have the appearance of multi-line
comments, with the difference being that they generate external documentation of your
source code.
/** This is a documentation comment */

/** This is also a


documentation comment */

02 Handout 1 *Property of STI


[email protected] Page 1 of 4
SH1801

III. Variables
• Variables store data for processing
• A variable is given a name (or identifier), such as area, age, height, and etc. The name uniquely
identifies each variable, assigning a value to the variable and retrieving the value stored.
• There are many data types for variables, some examples are:
o int – used to declare an integer (whole numbers) such as 112 and -763.
o double – for floating-point or real numbers with optional decimal points and fractional
parts in fixed or scientific notations, such as 3.1415 and -55.61.
o String – for sequence of characters such as “Hello” or “Good Morning!”. Text strings are
enclosed within straight double quotes ("").
• char – for variables that hold a single alphanumeric character, such as a, Z, or 1.
• boolean – only accepts two (2) possible values: true and false.
Examples of variable declarations:
class MyClass {
public static void main(String[ ] args) {
String name = "Archie";
int age = 18;
double score = 9.9;
char group = 'S';
boolean online = true;
}
}

IV. Primitive Operators


A. The Math operators
• Java provides a rich set of operators to use in manipulating variables. A value used on
either side of an operator is called an operand.
int x = 9 + 5;
• Addition – The plus sign (+) operator adds together two (2) values, such as two (2)
constants, a constant and a variable, or a variable and a variable.
int sum1 = 20 + 40;
int sum2 = sum1 + 23;
int sum3 = sum2 + sum2;
• Subtraction – The dash (–) operator subtracts one value from another.
int sum1 = 650 - 85;
int sum2 = sum1 - 30;
int sum3 = sum1 - sum2;
• Multiplication – The asterisk (*) operator multiplies two (2) values.
int sum1 = 70 * 2;
int sum2 = sum1 * 12;
int sum3 = sum1 * sum2;
• Division – The forward slash (/) operator divides one value by another.
int sum1 = 500 / 5;
int sum2 = sum1 / 2;
int sum3 = sum1 / sum2;

02 Handout 1 *Property of STI


[email protected] Page 2 of 4
SH1801

• Modulo – The modulo (or remainder) math operation performs an integer (whole number)
division of one value by another, and returns the remainder of that division. The operator
for the modulo operation is the percentage (%) character.
int value = 39;
int res = value % 8; // res is 7

V. Increment & Decrement


A. Increment Operators
• An increment or decrement operator provides a more convenient and compact way to
increase or decrease the value of a variable by one.
• For example, the statement x=x+1; can be simplified to ++x;
int test = 3;
++test; // test is now 4
• The decrement operator (--) is used to decrease the value of a variable by one.
int test = 3;
--test; // test is now 2

B. Prefix & Postfix


• The two (2) forms, prefix and postfix, may be used with both the increment and decrement
operators. With prefix form, the operator appears before the operand, while in postfix form,
the operator appears after the operand.
• Prefix – Increments the variable’s values and uses the new value in the expression.
int x = 34;
int y = ++x; // y is 35
• Postfix – the variable’s value is first used in the expression and is then increased.
int x = 17;
int y = x++; // y is 17

C. Assignment Operators
• The assignment operator (=), which assigns a value to a variable.
int value = 5;
• This assigned the value 5 to a variable called value of type int.
• Java provides a number of assignment operators to make it easier to write code.
• Addition and assignment (+=):
int num1 = 4;
int num2 = 8;
num2 += num1; // num2 = num2 + num1;

// num2 is 12 and num1 is 4

• Subtraction and assignment (-=):


int num1 = 4;
int num2 = 8;
num2 -= num1; // num2 = num2 - num1;

// num2 is 4 and num1 is 4

02 Handout 1 *Property of STI


[email protected] Page 3 of 4
SH1801

VI. Strings
A. String
• A String is an object that represents a sequence of characters. For example, “Hello” is a
string of five (5) characters.
String s = "MyMaster";
B. String Concatenation
• The + (plus sign) operator between string values adds them together to make a new
string. This process is called concatenation.
• The resulting string is the first string, put together with the second string.
String firstName, lastName;
firstName = "Aries”;
lastName = "Davis";

System.out.println("My name is " + firstName +" "+lastName);

// Prints: My name is Aries Davis

VII. Getting User Input


• While Java provides many different methods for getting user input, the Scanner object is
the most common, and perhaps the easiest to implement. Import the Scanner class to use
the Scanner object, as seen below:
import java.util.Scanner;
• In order to use the Scanner class, create an instance of the class by using the following
syntax:
Scanner myVar = new Scanner(System.in);
• You can now read in different kinds of input data that the user enters.
• Here are some methods that are available through the Scanner class:
o nextByte() – Read a byte
o nextShort() – Read a short
o nextlnt() – Read an int
o nextLong() – Read a long
o nextFloat() – Read a float
o nextDouble() – Read a double
o nextBoolean() – Read a boolean
o nextLine() – Read a complete line
o next() – Read a word
• Example of a program used to get user input:
import java.util.Scanner;

class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
System.out.println(myVar.nextLine());
}
}

Reference:
Deitel, H., & Deitel, P. (2014). Java: How to program-early objects (10th ed.). Prentice Hall.
SoloLearn.com – Java Programming. Retrieved on March 07, 2018 at https://www.sololearn.com/Play/Java#

02 Handout 1 *Property of STI


[email protected] Page 4 of 4

You might also like