Education logo

INTRODUCTION TO JAVA PROGRAMMING

Java Programming Language

By ReshmaPublished about a year ago 7 min read
7

What is Java?

Java is an object-oriented programming, multi-thread programming language developed by Sun Microsystems, in 1991 a company best known for its high-end Unix workstations. Modeled after C++, the Java language was designed to be small, simple and portable across platforms and operating system, both at the source at the binary level.

The Creation of Java

Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. This language was initially called “Oak” but was renamed “Java” in 1995.

Why Learn Java?

At the moment, probably the most compelling reason learn Java is that HotJava applets are written in java. Even if that were not the case, Java as a language has significant advantages over other languages and other programming environments that make it suitable for just about any programming task. Most of the mobile phones used now support java applications.

Features of Java

  1. Simple
  2. Portable
  3. Object Oriented
  4. Interpreted
  5. Network-Savvy
  6. High Performance
  7. Robust
  8. Multithreaded
  9. Secure
  10. Dynamic
  11. Architecture Neutral

Simple

The syntax used to Java is similar to C++. Java omits many rarely used, poorly understand, confusing features of C++ that, There is no need for header files, pointer arithmetic (or even a pointer syntax), structures, union, operators overloading, virtual base classes.

Object Oriented

Simply stated, object-oriented design is a technique for programming that focuses on the data (=objects) and on the interfaces to that object.

Network-Savvy

Java has an extensive library of routine for copying with TCP/Ip protocols like HTTP and FTP. Java applications can open and access objects across the Net via URLs with the same ease as when accessing a local file system. We have found the networking capabilities of java to be both strong and easy to use. The RMI (remote method invocation) mechanism enables communication between distributed objects.

Robust

The java compiler detects many problems that, in other languages, would show up only at runtime. And java does not support pointers so java programs have complete safety, because you can never access a bad pointer, make memory allocation errors, or have to protect against memory leaking away.

Secure

When you use a Java-compatible web browser, you can safely download Java applets without fear of viral infection or malicious intent, Java achieves this protection by confining a Java program to the Java execution environment and not allowing it access to other parts of the computer.

Architecture Neutral (Platform Independent)

Platform Independence is one of the most significant advantages that has over other programming languages, particularly for systems that need to work on many different platforms, Java is platform-independent at both the source and the binary level.

Interpreted and High Performance

As describe earlier, Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be interpreted on any system that provides a Java Virtual Machine. As explained earlier, while it is true that Java was engineered for interpretation, the Java Byte code was carefully designed so that it would be easy to translate directly into native machine code for very high performance.

Multithreaded

Java supports multithreaded programming, which allows you to write programs that do many things simultaneously. The Java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enable you to construct smoothly running interactive systems. Java’s easy to use approach to multithreading allows you to think about the specifics behavior of yours program, not the multitasking subsystem.

Dynamic

Java programs carry with them substantial amount of run-time type information that is used to verify and resolve accesses to object at run time. This makes it possible to dynamically link ode in a safe and expedient manner. This is crucial to the robustness of the applet environment, in which small fragments of bytecode may be dynamically updated on a running system.

Traditional Programs Vs Java Programs

Most modern languages (C & C++) are designed to be compiled, not interpreted mostly because of performance concerns.

The java development environment has two parts: a Java compile and a Java interpreter. The Java compiler takes your Java program and instead of generating machine codes from your sources files, it generates byte codes and the interpreter executes the Java program.

Java Programs

Having yours Java Programs in byte code form means that instead of being specific to any one system, your programs can be run on any platform and any operating or window system as long as the Java interpreter is available.

The disadvantage of using byte code is in execution speed. Because system-specific programs run directly on the hardware for which they are compiled, they run significantly faster than Java byte codes, which must be processed by the interpreter.

Java Is Object-Oriented

Object-oriented programming (OOP) technique is merely a way of organizing programs, and it can be accomplished using any language. Working with a real object-oriented language and programming environment, however, enables you to take full advantage of object-oriented methodology and its capabilities for creating flexible, modular programs and reusing code.

Many of Java’s object-oriented concepts are inherited from C++, the language on which it is based, but it borrows many concepts from other object-oriented languages as well. Like most object-oriented programming languages, Java includes a set of class libraries that provide basic data types, system input and output capabilities and other utility functions. These basic classes are part of the Java Development Kit, which also has classes to support networking, common Internet protocols, and user interface toolkit functions. Because these class libraries are written in Java, they are portable across platform all Java applications.

Getting Started with Programming in Java

There are two types of Java programs: A stand-alone Java application and an applet that you can view in either in the applet viewer (part of the JDK) or in a Java-capable browser. Although both these programs are extremely simple, they will give you an idea of what a Java program looks like s how to compile and run it.

Applet and Application

Applets, as you have learned are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the reader’s machine. Applets depend on a Java-capable browsers in order to run.

Java Applications are more general programs written in the Java Language. Java application don’t require a browser to run and in fact, Java can e used to create all kinds of applications that you would normally use a more conventional; programming language to create. HotJava itself is a Java application.

Creating a Java Application

public class programMain {

public static void main (String args []) {

System.out.println("HELLO! Welcome to my stories");

}

}

Once you finish typing the program, save the file

>javac programMain.java

>java programMain

[Note: Remember, the Java compiler and the Java interpreter are different things. You use the Java compiler(javac) for your source files to create class files, and you use the java interpreter (java) to actually run your class files.]

A Closer Looks at the First Sample Program (PprogramMain.java)

1. Comment – a comment describes or explains the operation of the program to anyone who is reading its source code.

// - Single Line Comment

/*…………*/ - Multi Line Comment

2. Packages – Collection of Classes and Methods. java. Lang (Default package) contains all the basic methods. Import – is used to include the package to the program

Import java.lang.*;

*All classes present in all the package.

3. Class Definition – All programs in java should start with a class definition.

Class Classname

{

4. main method (function) – this is the line at which the program will begin executing. All Java applications begins execution by calling main ().

Public static void main (String args [])

{

public - main() must be declared as public, since it must be called by code outside of its class when the program is started.

static - static allows main () to be called without having to instantiate a particular instance of the class.

void – void simply tells the compiler that main() does not return a value.

String args[] – args is an array of instance of the class String. Args receives any command-line arguments present when the program is executed.

5. System.out.println(“HELLO! Welcome to my stories");

This line outputs the string “HELLO! Welcome to my stories" followed by a new line on the screen.

6. } -> End of Main

7. } ->End of class

Literals

Literals are used to indicates= simple values. Literal is a programming language term, which essentially means that what you type is what you get. For example, if you type 4 in a have program, you automatically get an integer with the value 4. If you type ‘a’. you get a character with the value a.

The different types of literals are numbers, characters, string, and Boolean values.

Number Literals

There are several integer literal such as int, long, octal, hexadecimal.

Boolean Literals

Boolean literal consists of the keywords true and false. These keywords can be used anywhere you need a test or as the only possible value for Boolean variables.

Character Literals

Character Literals are expressed by a single character surrounded by single quotes: example: ‘#’, ‘3’, and so on. Characters are sorted as 16-bit Unicode character. The range of a char of 0 to 65,536. Table lists the special codes that can represent nonprintable characters, as well as character from the Unicode character set.

Character escape codes.

Escape Meaning

\n Newline

\t Tab

\b Backspace

\r Carriage return

\f Form feed

\\ Backslash

\’ Single quotes

\” Double Quotes

\ddd Octal

\xdd Hexadecimal

\udddd Unicode character

String Literals

A combination of character is a string. Strings in java are instances of the class String. Because string objects are real objects in java, they have methods that enable you to combine, test and modify strings very easily.

String literals consist of a series of characters inside double quotes:

“Hi, I’m a string literal.”

“”//an empty string

Strigs can contain character constants such as newline, tab and Unicode characters:

“A string with a \t tab in it”

“Nested strings are\” strings inside of\” other strings”

“This string brought to you by java\u 2122”

collegecourses
7

About the Creator

Reshma

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.