Download java notes for bca students vinoba bhave university and more Thesis Java Programming in PDF only on Docsity!
How Java Differs From C And C++
Although Java was modeled after C and C++ languages, it differs from C and
C++ in many ways. Java does not i a number of features available in C and
C++. For the benefit of C and C++ programmers, we point out here a few major
differences between C/C++ and Java language
How Java Differs From C:
Java and C
Java is not lot like C but the major difference between Java and C is that Java is
and object-oriented language and has mechanism to define classes and objects.
In an effort to build a simple and safe language, the Java team did not include
some of the C features in Java.
Java does not include the C unique statement keywords sizeof, and typedef.
Java does not contain the data type struct and union.
Java does not define the type modifiers keywords auto,extern,register,signed, and unsigned.
Java does not support an explicit pointer type.
Java does not have a preprocessor and therefore we cannot use # define, # include, and
# ifdef statements.
Java requires that the functions with no arguments must be declared with empty parenthesis and not
with the void keyword as done in C.
Java adds new operators such as instanceof and >>>.
Java adds labelled break and continue statements.
Java adds many features required for object-oriented programming.
How Java Differs From C++ :
Java and C++
Java is a true object-oriented language while C++ is basically C with object-
oriented extension. That is what exactly the increment operator ++ indicates.
C++ has maintained backward compatibility with C. Is is therefore possible to
write an old style C program and run it successfully under C++. Java appears to
be similar to C++ when we consider only the “extensions” part of C++.
However, some object - oriented features of C++ make the C++ code extremely
difficult to follow and maintain.
Listed below are some major C++ features that were intentionally omitted from
java or significantly modified.
Java does not support operator overloading.
Java does not have template classes as in C++.
Java does not support multiple inheritance of classes. This is accomplished using a new feature called
“Interface”.
Java does not support global variables. Every variable and method is declared within classes and
forms part of that class.
Java does not use pointers.
Java has replaced the destructor function with a finalize() function.
There are no header files in Java.
Java also adds some new features. While C++ is a superset of C, Java is neither
a superset nor a subset of C or C++. Java may be considered as a first cousin of
C++ and a second cousin of C
Java and Internet
View 1195
Java is strongly associated with the internet because of the first application
program is written in Java was hot Java.
Web browsers to run applets on the internet.
Internet users can use Java to create applet programs & run then locally using a
Java-enabled browser such as hot Java.
Java applets have made the internet a true extension of the storage system of the
local computer.
1. Java communicates with a web page through a special tag called
2. Java user sends a request for an HTML document to the remote computers net
browser.
3. The web-browser is a program that accepts a request, processes the request and
sends the required documents.
4. The HTML document is returned to that user browser.
5. The document contains the applet tag which identifies the applet. The corresponding
applet is transferred to the user computer.
6. The Java enabled browser on the user's computer interprets the byte code and
provide output.
Web Browsers
The internet is a vast sea of information represented in many formats and stored
on many computers. a browser is a software application used to locate, retrieve
and display content on the World Wide Web, including Web pages, images, video
and other files. As a client/server model, the browser is the client run on a
computer that contacts the Web server and requests information. The Web server
sends the information back to the Web browser which displays the results.
The browser application retrieves or fetches code, usually written in HTML
(HyperText Markup Language) and/or another language, from a web server,
interprets this code, and renders (displays) it as a Web page for you to view. on
the computer or another Internet-enabled device that supports a browser.
An example of Web Browsers:
Hot Java
Netscape Navigator
Internet Explorer
Google Chrome
Java Support System
Web Browser
local computer should be connected to the internet
Web Server
A program that accepts a request from a user and gives output as per the
requirement. Apache TomCat server is one of the major web servers.
Web Browser
The web browser is a software that will allow you to view web pages on the
internet. it is a program that you use to access the Internet. It reads and knows
how to display and download files that are put on servers for people to read. A
program that provides the access of WWW and runs java applets. Chrome and
Firefox are two major web browsers.
HTML
HTML is short for HyperText Markup Language. HTML is used to create
electronic documents (called pages) that are displayed on the World Wide Web.
Each page contains a series of connections to other pages called hyperlinks. Every
web page you see on the Internet is written using one version of HTML code or
another.
APPLET tag
- The user sends a request for an HTML document to the remote computer’s Web server. The Web server is a program that accepts a request, processes the request, and sends the request document.
- The HTML document is returned to the user’s browser. The document contains the APPLET tag, which identifies the applet.
- The corresponding applet byte code is transferred to the user’s computer. This bytecode had been previously created by the Java compiler using Java source code file for that applet.
- The Java enabled browser on the user’s computer interprets the byte codes and provides output.
- The user may have further interaction with the applet but with no further downloading from the provider’s Web server. This is because the bytecode contains all the information necessary to interpret the applet.
Using multiple classes in a Java
program
A Java program can contain any number of classes. Following Java program comprises of two
classes: Computer and Laptop. Both classes have their own constructors and a method. In the
main method, we create objects of two classes and call their methods.
Using two classes in Java program
class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
void laptop_method() {
System.out.println("99% Battery available.");
Import
statements
The next is the number of import statements, which is equivalent to the #include
statement in C++.
Example:
import calc.add;
Interface
statement
Interfaces are like a class that includes a group of method declarations. This is an
optional section and can be used only when programmers want to implement
multiple inheritances within a program.
Class Definition A Java program may contain multiple class definitions. Classes are the main and
important elements of any Java program. These classes are used to plot the
objects of the real world problem.
Main Method
Class
Since every Java stand-alone program requires the main method as the starting
point of the program. This class is essentially a part of Java program. A simple
Java program contains only this part of the program.
There may be many classes in the Java program, and only one class defines the main
method. Methods contain data type declaration and executable statements.
To understand the class structure and features I have given an example of Hello Java
program here. There are a few lines in the program and the major task of the program is to
print Hello Java text on the screen.
Sample Code of Java "Hello Java" Program
Example:
public class Hello
/* Author: www.w3schools.in
Date: 2017-08-
Description:
Writes the words "Hello Java" on the screen */
public static void main(String[] args)
System.out.println("Hello Java");
Program Output:
Hello Java
You have to keep in mind that, Java code is case sensitive.
To write Java program, you must have to define class first.
Let's look into various parts of the above Java program.
public class Hello This creates a class called Hello.
All class names required to start with a capital letter.
The word public means that it is accessible by any other classes.
/* Comments */ Comments are a way of explaining what makes a program. Comments are
ignored by the compiler and used by others to understand the code.
or
This is a comment block, which is ignored by the compiler. Comment can be
used anywhere in the program to add info about program or code block, which
will be helpful for developers to easily understand the existing code in the
future.
Braces Two curly brackets {...} are used to group all the commands together so it is
known that the commands belong to that class or method.
public static void The word public means that it is accessible by any other classes.
supported in Java include keywords, variables, constants, special characters,
operations etc.
When you compile a program, the compiler scans the text in your source code and
extracts individual tokens. While tokenizing the source file, the compiler recognizes and
subsequently removes whitespaces (spaces, tabs, newline and form feeds) and the text
enclosed within comments. Now let us consider a program.
//Print Hello
Public class Hello
Public static void main(String args[])
System.out.println(“Hello Java”);
The source code contains tokens such as public, class, Hello, {, public, static, void,
main, (, String, [], args, {, System, out, println, (, "Hello Java", }, }. The resulting tokens·
are compiled into Java bytecodes that is capable of being run from within an interpreted
java environment. Token are useful for compiler to detect errors. When tokens are not
arranged in a particular sequence, the compiler generates an error message.
Tokens are the smallest unit of Program There is Five Types of Tokens
1) Reserve Word or Keywords
2) Identifier
3) Literals
4) Operators
5) Separators
Java Keywords
keyword
Java Keywords must be in your information because you can not use them as a variable
name.
Java Reserved Keywords List
You can't use keyword as identifier in your Java programs, its reserved words in Java
library and used to perform an internal operation.
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
Examples of valid identifiers :
MyVariable
MYVARIABLE
myvariable
x
i
x
i
_myvariable
$myvariable
sum_of_array
geeks
Examples of invalid identifiers :
My Variable // contains a space
123geeks // Begins with a digit
a+c // plus sign is not an alphanumeric character
variable-2 // hyphen is not an alphanumeric character
sum_&_difference // ampersand is not an alphanumeric character
Reserved Words
Any programming language reserves some words to represent functionalities defined by that language.
These words are called reserved words.They can be briefly categories into two parts : keywords (50)
and literals (3).
keywords define functionalities and literals defines a value.
Identifiers are used by symbol tables in various analyzing phases(like lexical,syntax,semantic) of a compiler architecture.
Note : The keywords const and goto are reserved, even though they are not currently used. In place of
const, final keyword is used. Some keywords like strictfp are included in later versions of Java.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute,
you can also write an article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Literals in Java
Literal : Any constant value which can be assigned to the variable is called as literal/constant.
// Here 100 is a constant/literal.
int x = 100;
Integral literals
For Integral data types (bye, short, int, long), we can specify literals in 4 ways:-
1. Decimal literals (Base 10) : In this form the allowed digits are 0-9.
- int x = 101;
3. Octal literals (Base 8) : In this form the allowed digits are 0-7.
// floating-point literals public class Test { public static void main(String[] args) { int a = 101.230; // decimal-form literal int b = 0123.222; // It also acts as decimal literal int c = 0x123.222; // Hexa-decimal form System.out.println(a); System.out.println(b); System.out.println(c); } }
Run on IDE
Error: malformed floating point literal
NOTE: By default every floating point literal is of double type and hence we cant assign directly to float
variable. But we can specify floating point literal as float type by suffixed with f or F. We can specify explicitly floating point literal as double type by suffixed with d or D. Of course this convention is not required.
Char literal
For char data types we can specify literals in 4 ways:
1. Single quote : We can specify literal to char data type as single character within single quote.
- char ch = 'a';
3. Char literal as Integral literal : we can specify char literal as integral literal which represents
Unicode value of the character and that integral literals can be specified either in Decimal, Octal and Hexadecimal forms. But the allowed range is 0 to 65535.
- char ch = 062;
5. Unicode Representation : We can specify char literals in Unicode representation „\uxxxx‟.
Here xxxx represents 4 hexadecimal numbers.
- char ch = '\u0061';// Here /u0061 represent a.
7. Escape Sequence : Every escape character can be specify as char literals.
- char ch = '\n';
Example :
// Java program to illustrate the application of char literals public class Test { public static void main(String[] args) { char ch = 'a'; // signle character literl within signle quote char b = 0789; // It is an Integer literal with octal form char c = '\u0061'; // Unicode representation System.out.println(ch); System.out.println(b);
System.out.println(c); // Escape character literal System.out.println("" is a symbol"); } }
Run on IDE
a
error:Integer number too large
a
" is a symbol
String literal
Any sequence of characters within double quotes is treated as String literals.
String s = "Hello";
String literals may not contain unescaped newline or linefeed characters. However, the Java compiler will evaluate compile time expressions, so the following String expression results in a string with three lines of text:
Example:
String text = "This is a String literal\n"
// Java program to illustrate the application of String literals public class Test { public static void main(String[] args) { String s = "Hello";
// If we assign without "" then it treats as a variable // and causes compiler error String s1 = Hello;
System.out.println(s);
System.out.println(s1); } }
Run on IDE
Hello
error: cannot find symbol
symbol: variable Hello
location: class Test