Java Programming - Arvin Hipolito Assignment
Java Programming - Arvin Hipolito Assignment
Compared to C++ (another object-oriented language), Java code runs a little slower
(because of the JVM) but it is more portable and has much better security features. The
virtual machine provides isolation between an untrusted Java program and the PC
running the software. Java's syntax is similar to C++ but the languages are quite
different. For example, Java does not permit programmers to implement operator
overloading while C++ does. In addition, Java is a dynamic language where you can
safely modify a program while it is running, whereas C++ does not allow it. This is
especially important for network applications that cannot afford any downtime. Also, all
basic Java data types are predefined and not platform-dependent, whereas some data
types can change with the platform used in C or C++ (such as the int type).
Java programs are more highly structured than C++ equivalents. All functions (or
Java methods) and executable statements in Java must reside within a class while C++
allows function definitions and lines of code to exist outside of classes (as in C-style
programs). Global data and methods cannot reside outside of a class in Java, whereas
C++ allows this. These restrictions, though cumbersome at times, help maintain the
integrity and security of Java programs and forces them to be totally object-oriented.
Arvin L. Hipolito Computer Programming II
BSCS-2
/* Comments */
The compiler ignores comment block. Comment can be used anywhere in the
program to add info about the program or code block, which will be helpful for
developers to understand the existing code in the future easily.
Braces Two curly brackets {...} are used to group all the commands, so it is known
that the commands belong to that class or method.
String[] args
It is an array where each element of it is a string, which has been named as
"args". If your Java program is run through the console, you can pass the input
parameter, and main() method takes it as input.
System.out.println();
This statement is used to print text on the screen as output, where the system is
a predefined class, and out is an object of the PrintWriter class defined in the system.
The method println prints the text on the screen with a new line. You can also use print()
method instead of println() method. All Java statement ends with a semicolon.
Documentation Section
You can write a comment in this section. Comments are beneficial for the
programmer because they help them understand the code. These are optional, but we
Arvin L. Hipolito Computer Programming II
BSCS-2
suggest you use them because they are useful to understand the operation of the
program, so you must write comments within the program.
Package statement
You can create a package with any name. A package is a group of classes that
are defined by a name. That is, if you want to declare many classes within one element,
then you can declare it within a package. It is an optional part of the program, i.e., if you
do not want to declare any package, then there will be no problem with it, and you will
not get any errors. Here, the package is a keyword that tells the compiler that package
has been created.
It is declared as:
package package_name;
Import statements
This line indicates that if you want to use a class of another package, then you
can do this by importing it directly into your program.
import calc.add;
Interface statement
Interfaces are like a class that includes a group of method declarations. It's an
optional section and can be used when programmers want to implement multiple
inheritances within a program.
Class Definition
A Java program may contain several class definitions. Classes are the main and
essential elements of any Java program.
Every Java stand-alone program requires the main method as the starting point
of the program. This is an essential part of a Java program. There may be many classes
in a Java program, and only one class defines the main method. Methods contain data
type declaration and executable statements.
Arvin L. Hipolito Computer Programming II
BSCS-2
4. Find a sample program in java that will print the word Hello.
References:
https://www.sciencedirect.com/topics/computer-science/java-programming-language
https://www.w3schools.in/java-tutorial/program-structure/
https://www.programiz.com/java-programming/hello-world