Java
Java is a language that is purely object oriented, all code must be included in any class
It was defined by the company Sun Microsystems, which was acquired in 2009 by Oracle Corporation, formerly part of Silicon Valley, a maker of semiconductors and software; but the language remains in the public domain. In particular, Oracle offers in their website, for free, a JDK (Java Development Kit) which allows you to compile, debug, and run Java programs
If we have a program in a file programa.java we can compile it from the command line with:
This generates a collection of files extension .class, one for each class that appears in programa.java and with the name of the corresponding class
Only in one of the classes (in the active environment) must have a method called main (the main method); suppose that is in the class program, we will be able to run the program from the command line:
In the previous case, the extension would be optional
For this to work correctly, we have installed the JDK and make sure that the path stored in the variable CLASSPATH is in the path by default of the system
Normally the programs are developed in an environment of comprehensive programming that contains in addition to the functionality of the JDK, at least a program editor. Sun offers on their website an integrated environment for Java: Netbeans
The result of the compilation a java program (the files .class) is expressed in an intermediate language called bytecode
The bytecode is interpreted by the Java virtual machine (JVM), which is a program that is invoked by the command java in the command line
The different modern browsers include a JVM, so that it can run Java programs from the browser through Applets embedded within html pages
Although the Google Chrome browser has restricted its use for safety and the JVM is not included by default, in the event that the user wishes to use them, you should install a pluging to any third party or use a dedicated server for Java applications as for example Tomcat
Differences between Java and C++
The syntax of Java is inspired by C and is superficially very similar to C++ for that reason. But it also has important differences
Access modes in Java:
- Each attribute or method is associated with a permission
- In addition public and private (both without the two dots) is the mode by default package
In contrast, in C++, the default access mode is private
To use something by default in Java you use the package (packets), which is a simulation of the notion of namespace C++
To create or extend a package using the package:
- Must be in the 1st line of the source file (everything in that file belongs to the package)
- Will be followed by the name of the package that you want to use
If you want to use from a package, classes, attributes, methods from another package, use the command import (it is very similar to the using namespaces C++)
If the programmer does not define any package explicitly, the system creates one by default without name, that will contain all the names of the files (that do not have package explicit) that belong to that operating system directory
In conclusion, in case of the absence of packages explicit, the default access mode for a class, an attribute or a method, it is accessible from all files in a same directory
The classes in Java also have a mode of access: public, private, or default
If we find public when you define a class, has two consequences:
- It is accessible from any point, regardless of the packages
- Your name must be the file .java-what it contains; there can be only one public class per file
Comments
Java has three kinds of comments:
- The // he says until the end of the line
- The /* he says to the first */ (no nesting)
- The /** he says to the first **/