Java: A Beginner’s Introduction to Programming
Java Programming Language
Identifiers
An identifier is a name given to any element of a program.
Keywords
Predefined identifiers for the exclusive use of the language.
Rules for Identifiers
- Can be any length of characters.
- The first character must be a letter.
- Should not include spaces.
- Can be uppercase or lowercase.
Case Sensitivity
Java is case-sensitive; identifiers like ‘age’, ‘Age’, and ‘AGE’ are considered different.
Points to Remember
- In class names, each word starts with a capital letter.
- The first word is all lowercase, and the second begins with a capital letter.
- Variable names use nouns or adjectives.
- Method names use a verb followed by the name of the variable that it affects.
- Avoid using underscores in general.
Data Types
A constraint for the interpretation and manipulation of data representation.
Primitive Data Types
These are integrated into the language and are not about objects.
- boolean
- byte
- short
- int
- long
- char
- double
- float
Class Data Type
A variable can have a class as its type, in which case the variable relates to an object of that class or its subclasses.
Comments
Information within the code, often used to explain what is being done. There are two types:
- // Single-line comment
- /* Multi-line comment */
- /** Documentation comment */
Variable Scope
- Local: Cannot be modified from another procedure.
- Global: Available in any part of the program.
- Instance: Each instance of a class will have these variables.
Expressions, Operators, and Operands
- An expression can be a constant, variable, or a combination of constants.
- Symbols that represent operations permitted on the language.
- Operands are arguments of an operator.
Types of Operators
- Arithmetic
- Relational
- Logical
- Assignment
Control Structures
- Conditional: The statement can have 1 or 2 values (true or false).
- Cyclical: Continues until a certain number of cycles is reached.
Structured/Modular Programming
Minimizes bugs by using modules with functions called to run the program.
Object-Oriented Programming (OOP)
A programming paradigm for solving problems.
Relationship between OOP and Structured/Modular Programming
OOP was based on that type of programming.
Difference between Class and Object
Objects belong to the class, and the class contains the characteristics of objects.
Main Element of OOP
Classes
Inheritance
Provides the ability to define new classes based on existing classes.
Method
Determines how the object has to act when receiving a message.
Parts of a Method
- Method header
- Access modifier
- Return type
- Method identifier
- Formal parameters
- Declaration of variables
- Method body
- Return value
Attribute
Each of the data (variables or fields) that make up a class.
Difference between Static and Final Attributes
The final value cannot be changed, and the static value is compatible on all objects.
Modifiers: Private and Public
Public can be accessed by any method of the project, and private methods are only for the same class.
Method Call
When the implementation is complete, the result is returned, and control is returned to the code segment from which it was invoked.