PROGRAMMING LANGUAGE BASICS Part 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PROGRAMMING LANGUAGE BASICS (Part 1)

Object - these have states and behaviors. It is an instance of a class.

Class - it can be defined as a template / blueprint that describes the behavior


/ state that the object of its type supports.

Methods - it is basically a behavior.


- it is where the logics are written, data is manipulated and all the
actions are executed.

Instance Variable - it is where values are assigned to create an object’s state.

Case Sensitivity - it states that Java is case sensitive, which means identifiers Hello
and hello would have different meanings in Java.

Class Names - the first letter should be in Upper Case. If several words are used to
form a name of the class, each inner word’s first letter should be in
Upper Case.

Method Names - these should start with a Lower Case letter. If several words are
used to form the name of the method, then each inner word’s first
letter should be in Upper Case.

Program File Name - the name of the program file should exactly match the class name.

Public static void main (String - Java program processing starts from the main() method which is a
args []) mandatory part of every Java program.

Identifiers - names used for classes, variables, and methods.


- should begin with a letter (A to Z or a to z), currency character ($)
or an underscore (_).
- these are case sensitive.

Modifiers - these are used to modify classes, methods,etc.

Two Categories of Modifiers

1. Access Modifiers - Java provides a number of access modifiers to set access levels for
classes, variables, methods and constructors.

Four Access Levels

- Visible to the package, the default. No modifiers are needed.


- Visible to the class only (private).
- Visible to the world (public).
- Visible to the package and all subclasses (protected)
.
2. Non-access Modifiers - Java provides a number of non-access modifiers to achieve many
other functionality.
Types of Modifiers

* Static Modifiers - used for creating class methods and variables.

*Final Modifiers - for finalizing the implementations of classes, methods and variables.

*Abstract Modifiers - used for creating abstract classes and methods.

*Synchronized and Volatile - used for threads.


Modifiers

Variable - it is just a memory location (or several consecutive locations treated


as a unit) that has been given a name so that it can be easily referred
to and used in a program.

Two Data Types Available in Java


A. Primitive Data Types
1. Byte - it is an 8-bit signed two’s complement integer
- minimum value is -128 (-27)
- maximum value is 127 (27-1)
- default value is 0
- it is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an integer.

2. Short - it is a 16-bit signed two's complement integer


- minimum value is -32,768 (-2^15)
- maximum value is 32,767 (inclusive) (2^15 -1)
- it can also be used to save memory as byte data type.
- it is 2 times smaller than an integer.
- Default value is 0.

3. Int - is a 32-bit signed two's complement integer.


- minimum value is - 2,147,483,648 (-2^31)
- maximum value is 2,147,483,647(inclusive) (2^31 -1)
- it is generally used as the default data type for integral values unless
there is a concern about memory.
- the default value is 0.

4. Long - it is a 64- bit signed two’s complement integer.


- minimum value is -9,223,372,036,854,775,808 (-2^63).
- maximum value is 9,223,372,036,854,775,807 (inclusive) (2^63 -1).
- this type is used when a wider range than int is needed.
- default value is 0L.

5. Float - e is a single-precision 32-bit IEEE 754 floating point.


- it is mainly used to save memory in large arrays of floating point
numbers
- default value is 0.0f
- it is never used for precise values such as currency
6. Double - it is a double-precision 64-bit IEEE 754 floating point
- this data type is generally used as the default data type for decimal
values, generally the default choice
- it should never be used for precise values such as currency
- default value is 0.0d

7. Boolean - it represents one bit of information


- there are only two possible values: true and false
- this data type is used for simple flags that track true/false
conditions
- default value is false

8. Char - is a single 16-bit Unicode character


- minimum value is '\u0000' (or 0)
- maximum value is '\uffff' (or 65,535 inclusive)
- it is used to store any character

B. Reference Data Type - these are created using defined constructors of the classes. They are
used to access objects. These variables are declared to be of a specific
type that cannot be changed.
- default value is null.
- can be used to refer any object of the declared type or any
compatible type.

Literal - it is a source code representation of a fixed value. They are


represented directly in the code without any computation.

String Literals - these are specified like they are in most other languages by
enclosing a sequence of characters between a pair of double quotes.

Variable - it provides us with named storage that our programs can manipulate.
- it has a specific type which determines the size and layout of the
its memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the it.
Three Kinds of Variables in Java

1. Local Variables - these are declared in methods, constructors, or blocks.


- these are created when the method, constructor or block is entered
and the variable will be destroyed once it exits the method,
constructor, or block.
- these are visible only within the declared method, constructor, or
block.
- these are implemented at a stack level internally/
- there are no default value.

2. Instance Variables - these are declared in a class, but outside a method, constructor or
any block.
- these are created when an object is created with the use of the
keyword 'new' and destroyed when the object is destroyed.
- hold values that must be referenced by more than one method,
constructor or block, or essential parts of an object's state that must be
present throughout the class.
- these can be declared in class level before or after use.
- these are visible for all methods, constructors and block in the class.
Normally, it is recommended to make these variables private (access
level). However, visibility for subclasses can be given for these
variables with the use of access modifiers.
- these have default values. For numbers, the default value is 0, for
booleans it is false, and for object references it is null. Values can be
assigned during the declaration or within the constructor.
- these can be accessed directly by calling the variable name inside
the class. However, within static methods (when instance variables
are given accessibility), they should be called using the fully qualified
name
.
3. Class / Static Variables - these are declared with the static keyword in a class, but outside a
method, constructor or a block.
- these are rarely used other than being declared as constants.
- these are stored in the static memory. It is rare to use static variables
other than declared final and used as either public or private
constants.
- these are created when the program starts and destroyed when the
program stops.

Constants - these are variables that are declared as public/private, final, and
static.
- these never change from their initial value.
Java Operators

1. Arithmetic Operators - these are used in mathematical expressions in the same way that
they are used in algebra.

2. Relational Operators
3. Bitwise Operators - this works on bits and performs bit-by-bit operation.

4. Logical Operators
5. Assignment Operators
6. Miscellaneous Operators

Conditional Operator - other term for ternary operator.


- this operator consists of three operands and is used to evaluate
Boolean expressions.

Instanceof Operator - this is only used for object reference variables.


- the operator checks whether the object is of a particular type (class
type or interface type).

Projects - it contains a tree view of the components of the project, including


source files, libraries that your code depends on, and so on.

Source editor - it contains a file called HelloWorldApp.java open.

Navigator - it is used to quickly navigate between elements within the selected


class.

Constants

e (Math.E) - returns a double representation of the base of the natural logarithm.

Pi (Math.PI) - returns a double representation of the ratio of the area of a circle to


its diameter.

infinity - returns a double representation of positive infinity.


(Double.POSITIVE_INFINITY)

- infinity - returns a double representation of negative infinity.


(Double.NEGATIVE_INFINITY)

Not a Number (Double.NaN) - returns a double representation of an IEEE NaN. The NaN is often
generated during floating point calculations to signify n exceptional
situation.
Functions

abs (Math.abs (value)) - returns the absolute integer value of a.

arc cosine (Math.acos (value)) - returns the arc cosine of a, in the range of 0.0 through Pi.

arc sine (Math.asin (value)) - returns the arc sine of a, in the range of -Pi/2 through Pi/2.

arc tangent (Math.atan (value)) - returns the arc tangent of a, in the range of -Pi/2 through Pi/2.

two argument arc tangent - returns the polar angle of the rectangular coordinates (x,y).
(Math.atan2 (x,y))

ceiling (Math.ceil (value)) - returns the smallest whole number greater than or equal to a.

cosine (Math.cos (value)) - returns the trigonometric cosine of an angle.

exp (Math.exp (value)) - returns the exponential number e (2.718…) raised to the power of a.

floor (Math.floor (value)) - returns the “floor” or largest whole number less than or equal to a.

logarithm (Math.log (value)) - returns the natural logarithm (base e) of value. To get the base 10
logarithm (ln), use Math.log (value) / Math.log (10).

max (Math.max (value_1, value - returns the greater of value_1 and value_2.
_2))

min (Math.min (value_1, - returns the lesser of value _1 and value_2.


value_2))

power (Math.pow (value_1, - returns value_1 to the power of value _2.


value_2)

random number - returns a pseudo-random number between 0.0 and 1.0.


(Math.random())

round (Math.round (value) - rounds off a floating point value by first adding 0.5 to it and then
returning the largest integer that is less than or equal to this new
value.

sine (Math.sin (value)) - returns the trigonometric sine of angle.

square root (Math.sqrt (value)) - returns the square root of a.

tangent (Math.tan (value)) - returns the trigonometric tangent of an angle.

You might also like