Computer Programming 2
The Java programming language is
• Java is a popular programming language, created in 1995.
• It is owned by Oracle, and more than 3 billion devices run Java.
• It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
Why use java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice
versa
Java Syntax
• Main Method: The main method is the entry point for Java programs. It has a specific
signature:
• Comments in Java are specified using // for single-line comments or /* */ for multi-line
comments.
• Variables: Variables are declared with a data type and an identifier.
• Data Types: Java has various data types, including primitive types (e.g., int, double, boolean)
and reference types (e.g., String, custom classes).
• Control Flow Statements: Java supports standard control flow statements like if, else, for,
while, and switch.
• Object Instantiation: Creating objects involves using the new keyword.
• Methods: Methods are declared within classes and can take parameters and return values.
• Packages and Imports: Java code is organized into packages, and classes within packages are
imported as needed.
• Exception Handling: Exceptions are handled using try, catch, finally, and throw blocks.
Data Types
Integral Types:
• Byte
• short
• Int
• Long
Floating-Point Types:
• Float
• Double
Characters:
• Char
• String
Boolean Type:
• Boolean
Arrays in Java have a fixed size once they are initialized.
Array indices start from 0.
Arrays can store primitive data types or objects.
Multi-dimensional arrays are arrays of arrays
What is a Java Methods?
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as functions.
• Why use methods? To reuse code: define the code once, and use it many times.
Parameters and Arguments
• Information can be passed to methods as parameter. Parameters act as variables inside the
method.
• Parameters are specified after the method name, inside the parentheses. You can add as
many parameters as you want, just separate them with a comma.
Return Values
• The void keyword, used in the examples above, indicates that the method should not return
a value. If you want the method to return a value, you can use a primitive data type (such as
int, char, etc.) instead of void, and use the return keyword inside the method: