Java Tutorial (Programing Language)

By: Luis A. Sierra


Introduction

Java is used to build Android apps, desktop and web apps, enterprise backend systems, and cloud-based software.

Java is in high demand with many job opportunities in software development.

Java has popular frameworks like Spring and Hibernate which makes Java powerful for enterprise applications.

Java supports object-oriented programming for clean and reusable code.
It runs on all platforms Windows, Mac, and Linux using the JVM.

Top companies like Amazon, Netflix, and LinkedIn use Java.

What is Java


Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).

Java syntax and structure is similar to C-based languages like C++ and C#. Its robustness, platform-independent compatibility, and strong memory management have made it a go-to language for developers worldwide.

History

Java was developed by James Gosling in the early 1990s. The team initiated this project to develop a language for digital devices such as set-top boxes, televisions, etc. Originally, C++ was considered to be used in the project, but the idea was rejected for several reasons (For instance, C++ required more memory). Gosling endeavoured to alter and expand C++; however, before long surrendered that for making another stage called Green. James Gosling and his team called their project “Greentalk” and its file extension was .g,t and later became to known as “OAK”.

The name Oak was used by Gosling after an oak tree that remained outside his office. Also, Oak is an image of solidarity and was picked as a national tree of numerous nations like the U.S.A., France, Germany, Romania, etc. But they had to later rename it as JAVA as it was already a trademark by Oak Technologies. “JAVA” Gosling and his team did a brainstorming session, and after the session, they came up with several names such as JAVA, DNA, SILK, RUBY, etc. The name "Java" was decided after much discussion since it was so unique.

The name Java originates from a sort of espresso bean, Java. Gosling came up with this name while having a coffee near his office. Java was created on principles like Robust, Portable, Platform Independent, High Performance, Multithread, etc., and was called one of the Ten Best Products of 1995 by TIME MAGAZINE. Currently, Java is used in internet programming, mobile devices, games, e-business solutions, etc.

IDEs

Jave Get Started

Hello, World! It is the first basic program in any programming language. Let's write the first program in the Java programming language.

public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!!!");
}
}

Copy the code below and run it at: https://www.onlineide.pro/playground/java


Java Comments

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line comments starts with two forward slashes (//).

public class Main {
public static void main(String[] args) {
// This is a comment
System.out.println("Welcome to http://interesting-homemade-projects");
}
}

Copy the code below and run it at: https://www.onlineide.pro/playground/java

Multi-line comments start with  /* and ends with */.

public class Main {
public static void main(String[] args) {
/* The code below will print the url of my web site
to the screen */
System.out.println("Welcome to http://interesting-homemade-projects");
}
}



Copy the code below and run it at: https://www.onlineide.pro/playground/java

Java Variables

Variables are the "place" where we can store the data that the program needs.

he variables have four characteristics:
  • A name
  • A type of data
  • Access Modifiers
  • A lifetime
All variables have a name, this name is what we are going to call it by so that it returns its value. Also when declaring it we must assign a data type, for example:

data type name;

If I were programming and it were an integer variable, for example, it would be:

int x;
double y;
boolean f;

Access Modifiers

In Java, Access Modifiers are the keywords and those are useful to define an accessibility level for all the types and type members.

By specifying the access level for all the types and type members, we can control whether they can be accessed in other classes or in current assembly or in other assemblies based on our requirements.

The following are the different types of access modifiers available in a Java programming language.
  • Public
  • Private
  • Protected
Using these four access modifiers, we can specify the following six levels of accessibility for all types and type members based on our requirements.


Constants


Constants are fixed values that are not changed during the execution of the program. These fixed values are also referred to as literals.

Constants can be of any basic data kinds, such as integers, characters, floating points, or string literals. Enumeration constants are also available.

Constants are processed similarly to regular variables, except that their values cannot be changed after they are defined.

Some examples of Constants:

public static final int THOUSAND = 1000;
public static final double PI = 3.14;
public static final String AUTHOR_NAME = "Luis A. Sierra";

Case Sensitivity

Case sensitivity defines whether uppercase and lowercase letters are treated as distinct or equivalent.

"Java : is case sensitive"


Write the code below and run it at: https://www.onlineide.pro/playground/java


Data Type

The following table shows the available data types in a visual basic programming language with memory size and range of values.


Example: How to use a Java Data Types:'


Write the code below and run it at: https://www.onlineide.pro/playground/java

Variable initialization

Initialization is the process of assigning a value to the Variable.

Java: every variable must be EXPLICITLY initialized before being used.



Write the code below and run it at: https://www.onlineide.pro/playground/java

Type Conversions

Type Conversion or Type Casting is the process of converting a variable of one predefined type into another.

If these data types are compatible with each other, the compiler automatically converts them and if they are not compatible, then the programmer needs to typecast them explicitly.

Java facilitates type conversion in two forms:
  1. Implicit (Automatic) Type Conversion
  2. Explicit Type Conversion

Implicit:

The following table shows some possible implicit numeric conversions supported in Java:


Example:

Write the code below and run it at: https://www.onlineide.pro/playground/java

Explicit:

The following table shows some of the explicit numeric conversions for which there is no implicit conversion:


Example:
Write the code below and run it at: https://www.onlineide.pro/playground/java

Example 2:


Write the code below and run it at: https://www.onlineide.pro/playground/java

Parsing:

In Java, "parsing" refers to the process of analyzing a string or other data structure to extract specific information and convert it into a different format or object. This is a fundamental concept in programming, enabling programs to interpret and work with various types of input.


Example:

Write the code below and run it at: https://www.onlineide.pro/playground/java

MORE EXAMPLES:

* Convert to String

Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to Integer

Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to Double


Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to Boolean

Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to Date


Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to Time


Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to DateTime


Write the code below and run it at: https://www.onlineide.pro/playground/java

 * Format DateTime


Write the code below and run it at: https://www.onlineide.pro/playground/java

* DayName of DateTime


Write the code below and run it at: https://www.onlineide.pro/playground/java

* Convert to ASCII


Write the code below and run it at: https://www.onlineide.pro/playground/java

* String Replace


Write the code below and run it at: https://www.onlineide.pro/playground/java

* String Length


Write the code below and run it at: https://www.onlineide.pro/playground/java

* String Copy


Write the code below and run it at: https://www.onlineide.pro/playground/java

Arrays


An array is a data structure consisting of a collection of elements, of same memory size, each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index.

Example: How to use a Java Arrays.


Write the code below and run it at: https://www.onlineide.pro/playground/java

Operators


Is a character that represents a specific mathematical or logical action or process.

In Java different types of operators are available; those are:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
The following table lists the different of arithmetic operators available in Java.


The following table lists the different types of assignment operators available in Java.


The following table lists the different types of comparison operators available in Java.


The following table lists the different types of  logical operators available in Java.

The following table lists the different types of bitwise operators available in Java.


if/else Statement


The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.

Example:


Write the code below and run it at: https://www.onlineide.pro/playground/java

Case Statement

The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement)

Example:


Write the code below and run it at: https://www.onlineide.pro/playground/java

For Statement

The for statement, also called the for loop, lets you repeat a statement or compound statement a specified number of times.

Example - Up:


Write the code below and run it at: https://www.onlineide.pro/playground/java

Example - Down:


Write the code below and run it at: https://www.onlineide.pro/playground/java

While Statement

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

Example:


Write the code below and run it at: https://www.onlineide.pro/playground/java

Method

A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.

Note: Methods do not return value.

Example:

Write the code below and run it at: https://www.onlineide.pro/playground/java

Function

A function, subprogram, procedure, method, routine or subroutine is a callable unit that has a well-defined behavior and can be invoked by other software units to exhibit that behavior.

Note: functions return  a value.

Exmaple:
Write the code below and run it at: https://www.onlineide.pro/playground/java

Class


A class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

Example:

Class1.java

Main.java


Write the code below and run it at: https://www.onlineide.pro/playground/java

Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.

Example:

Class1.java


Main.java


Write the code below and run it at: https://www.onlineide.pro/playground/java

Inheritance

Inheritance is one of the core features of object-oriented programming. It's a programming procedure that allows you to reuse code by referencing the behaviors and data of an object. In other words, a class that inherits from another class shares all the attributes and methods of the referenced class.

Example:

Class1.java
Class2.java

Main.java


Write the code below and run it at: https://www.onlineide.pro/playground/java

Constructor

In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

Example:

Class1.java
Main.java


Write the code below and run it at: https://www.onlineide.pro/playground/java

Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Example:

Class1.java
Class2.java
Class3.java
Class4.java
Main.java
Write the code below and run it at: https://www.onlineide.pro/playground/java

Exception handling

Exception is typically used to denote a data structure storing information about an exceptional condition. One mechanism to transfer control, or raise an exception, is known as a throw; the exception is said to be thrown. Execution is transferred to a catch.

Example:


Write the code below and run it at: https://www.onlineide.pro/playground/java

Post a Comment

Previous Post Next Post