Java what type of language




















Statically Typed: When dealing with data in a statically typed languages like Java, we have to declare what each variable will hold. For example, this variable will hold numbers; that other variable will hold text, and another variable will hold dates, and so on. This means that a statically typed language has a bit more structure to it. There are certain mistakes a programmer can make, which can be caught by the development tools we use with Java before you even run the program.

I tend to prefer statically typed languages. On the one hand, some of the languages built for JVM, such as Scala and Groovy, are languages created to run within the Java environment. One might argue that Java had a great deal of influence on the development of C. C also also helped make improvements on Java, so those two languages have gone back and forth influencing each other.

There is no technical relationship between Java and JavaScript at all. JavaScript was developed by Netscape in the mids, originally called LiveScript. Nobody was using it, so Netscape saw Java getting all this publicity and hype, and they basically rebranded Livescript as JavaScript to siphon off some of that excitement.

And it worked — JavaScript became popular. But from a technical perspective there is no relationship between the two, they just bear similar names. The only technical similarity is that they both derive their syntax from the C programming language. From the moment we founded Tech Elevator , we recognized that our prospective students were coming to us not to acquire new hobby or because of academic curiosity, but because they were interested in a new job and a new career.

We know we can teach people how to code using any programming language, and once they learn their first programming language, learning a second or third one is much easier. We saw that Java was the number one in-demand language in the country.

The reverse is true as well: the methods of the containing type have access to all members of a static member type, including the private members. A static member type even has access to all the members of any other static member types, including the private members of those types. A static member type can use any other static member without qualifying its name with the name of the containing type.

Static member types, on the other hand, are members and so can use any access control modifiers that other members of the containing type can. These modifiers have the same meanings for static member types as they do for other members of a type. Recall that all members of interfaces and annotations are implicitly public , so static member types nested within interfaces or annotation types cannot be protected or private.

For example, in Example , the Linkable interface is declared public , so it can be implemented by any class that is interested in being stored on a LinkedStack. In code outside the containing class, a static member type is named by combining the name of the outer type with the name of the inner type e. Under most circumstances, this syntax provides a helpful reminder that the inner class is interconnected with its containing type.

However, the Java language does permit you to use the import directive to directly import a static member type:. The nested type can then be referenced without including the name of its enclosing type e. However, importing a nested type obscures the fact that that type is closely associated with its containing type—which is usually important information—and as a result it is not commonly done.

A nonstatic member class is a class that is declared as a member of a containing class or enumerated type without the static keyword:. If a static member type is analogous to a class field or class method, a nonstatic member class is analogous to an instance field or instance method. An instance of a nonstatic member class is always associated with an instance of the enclosing type. The code of a nonstatic member class has access to all the fields and methods both static and non- static of its enclosing type.

Several features of Java syntax exist specifically to work with the enclosing instance of a nonstatic member class. Example shows how a member class can be defined and used. This example extends the previous LinkedStack example to allow enumeration of the elements on the stack by defining an iterator method that returns an implementation of the java.

Iterator interface. The implementation of this interface is defined as a member class. Notice how the LinkedIterator class is nested within the LinkedStack class. Because LinkedIterator is a helper class used only within LinkedStack , having it defined so close to where it is used by the containing class makes for a clean design, just as we discussed when we introduced nested types.

Like instance fields and instance methods, every instance of a nonstatic member class is associated with an instance of the class in which it is defined. This means that the code of a member class has access to all the instance fields and instance methods as well as the static members of the containing instance, including any that are declared private. This crucial feature was already illustrated in Example Here is the LinkedStack. LinkedIterator constructor again:. This single line of code sets the current field of the inner class to the value of the head field of the containing class.

The code works as shown, even though head is declared as a private field in the containing class. A nonstatic member class, like any member of a class, can be assigned one of the standard access control modifiers. In Example , the LinkedIterator class is declared protected , so it is inaccessible to code in a different package that uses the LinkedStack class but is accessible to any class that subclasses LinkedStack.

Member classes have two important restrictions:. A nonstatic member class cannot have the same name as any containing class or package. This is an important rule, one that is not shared by fields and methods. Nonstatic member classes cannot contain any static fields, methods, or types, except for constant fields declared both static and final.

The most important feature of a member class is that it can access the instance fields and methods in its containing object. We saw this in the LinkedStack. Linked Iterator constructor of Example :.

In this example, head is a field of the enclosing LinkedStack class, and we assign it to the current field of the LinkedIterator class which is a member of the nonstatic member class. If we want to use explicit references, and make use of this , then we have to use a special syntax for explicitly referring to the containing instance of the this object. For example, if we want to be explicit in our constructor, we can use the following syntax:.

The general syntax is classname. Note that member classes can themselves contain member classes, nested to any depth. However, because no member class can have the same name as any containing class, the use of the enclosing class name prepended to this is a perfectly general way to refer to any containing instance.

We notice that a top-level class can extend a member class. With the introduction of nonstatic member classes, two separate hierarchies must be considered for any class. The first is the inheritance hierarchy , from superclass to subclass, that defines the fields and methods a member class inherits.

The second is the containment hierarchy , from containing class to contained class, that defines a set of fields and methods that are in the scope of and are therefore accessible to the member class.

It is important to be familiar with the properties and rules of thumb that the two hierarchies have:. The two hierarchies are entirely distinct from each other; it is important that you do not confuse them. Refrain from creating naming conflicts, where a field or method in a superclass has the same name as a field or method in a containing class.

If such a naming conflict does arise, the inherited field or method takes precedence over the field or method of the same name in the containing class. Inherited fields and methods are in the scope of the class that inherits them and take precedence over fields and methods by the same name in enclosing scopes. To prevent confusion between the class hierarchy and the containment hierarchy, avoid deep containment hierarchies.

If a class is nested more than two levels deep, it is probably going to cause more confusion than it is worth.

If a class has a deep class hierarchy i. A local class is declared locally within a block of Java code rather than as a member of a class. Only classes may be defined locally: interfaces, enumerated types, and annotation types must be top-level or static member types. Typically, a local class is defined within a method, but it can also be defined within a static initializer or instance initializer of a class. Just as all blocks of Java code appear within class definitions, all local classes are nested within containing blocks.

For this reason, local classes share many of the features of member classes. It is usually more appropriate to think of them as an entirely separate kind of nested type. The defining characteristic of a local class is that it is local to a block of code. Like a local variable, a local class is valid only within the scope defined by its enclosing block. Example shows how we can modify the iterator method of the Linked Stack class so it defines LinkedIterator as a local class instead of a member class.

By doing this, we move the definition of the class even closer to where it is used and hopefully improve the clarity of the code even further. For brevity, Example shows only the iterator method, not the entire LinkedStack class that contains it. Local classes have the following interesting features:. Like member classes, local classes are associated with a containing instance and can access any members, including private members, of the containing class.

In addition to accessing fields defined by the containing class, local classes can access any local variables, method parameters, or exception parameters that are in the scope of the local method definition and are declared final.

Local classes are subject to the following restrictions:. The name of a local class is defined only within the block that defines it; it can never be used outside that block.

Note, however, that instances of a local class created within the scope of the class can continue to exist outside of that scope. This situation is described in more detail later in this section. Local classes cannot be declared public , protected , private , or static. Like member classes, and for the same reasons, local classes cannot contain static fields, methods, or classes. The only exception is for constants that are declared both static and final. As noted earlier, a local class can use the local variables, method parameters, and even exception parameters that are in its scope but only if those variables or parameters are declared final.

This is because the lifetime of an instance of a local class can be much longer than the execution of the method in which the class is defined. In discussing nonstatic member classes, we saw that a member class can access any members inherited from superclasses and any members defined by its containing classes.

The same is true for local classes, but local classes can also access final local variables and parameters. Example illustrates the different kinds of fields and variables that may be accessible to a local class:.

A local variable is defined within a block of code that defines its scope , and outside of that scope, a local variable cannot be accessed and ceases to exist. Any code within the curly braces that define the boundaries of a block can use local variables defined in that block. This type of scoping, which is known as lexical scoping , just defines a section of source code within which a variable can be used. It is common for programmers to think of such a scope as temporal instead—that is, to think of a local variable as existing from the time the JVM begins executing the block until the time control exits the block.

This is usually a reasonable way to think about local variables and their scope. The introduction of local classes confuses the picture, however. To see why, notice that instances of a local class can have a lifetime that extends past the time that the JVM exits the block where the local class is defined.

This can cause effects that some developers initially find surprising. This is because local classes can use local variables, and so they can contain copies of values from lexical scopes that no longer exist. This can been seen in the following code:. To make sense of this code, remember that the lexical scope of the methods of a local class has nothing to do with when the interpreter enters and exits the block of code that defines the local class.

Each instance of a local class has an automatically created private copy of each of the final local variables it uses, so, in effect, it has its own private copy of the scope that existed when it was created. Closures are useful in some styles of programming, and different programming languages define and implement closures in different ways. Java implements closures as local classes, anonymous classes, and lambda expressions. An anonymous class is a local class without a name.

It is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of Java code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call.

Consider Example , which shows the LinkedIterator class implemented as an anonymous class within the iterator method of the LinkedStack class. Compare it with Example , which shows the same class implemented as a local class. As you can see, the syntax for defining an anonymous class and creating an instance of that class uses the new keyword, followed by the name of a class and a class body definition in curly braces.

If the name following the new keyword is the name of a class, the anonymous class is a subclass of the named class. If the name following new specifies an interface, as in the two previous examples, the anonymous class implements that interface and extends Object. Because an anonymous class has no name, it is not possible to define a constructor for it within the class body. This is one of the basic restrictions on anonymous classes. Any arguments you specify between the parentheses following the superclass name in an anonymous class definition are implicitly passed to the superclass constructor.

Anonymous classes are commonly used to subclass simple classes that do not take any constructor arguments, so the parentheses in the anonymous class definition syntax are often empty. In the previous examples, each anonymous class implemented an interface and extended Object. Because the Object constructor takes no arguments, the parentheses were empty in those examples.

Because an anonymous class is just a type of local class, anonymous classes and local classes share the same restrictions. An anonymous class cannot define any static fields, methods, or classes, except for static final constants. Interfaces, enumerated types, and annotation types cannot be defined anonymously. Also, like local classes, anonymous classes cannot be public , private , protected , or static. The syntax for defining an anonymous class combines definition with instantiation. Using an anonymous class instead of a local class is not appropriate if you need to create more than a single instance of the class each time the containing block is executed.

Because an anonymous class has no name, it is not possible to define a constructor for an anonymous class. If your class requires a constructor, you must use a local class instead. However, you can often use an instance initializer as a substitute for a constructor. An anonymous class cannot define a constructor, so it only has a default constructor. By using an instance initializer, you can get around the fact that you cannot define a constructor for an anonymous class.

The preceding sections explained the features and behavior of the four kinds of nested types. That should be all you need to know about nested types, especially if all you want to do is use them. You may find it easier to understand nested types if you understand how they are implemented, however. In order to make a nested type behave as if it is actually defined inside another class, the Java compiler ends up inserting hidden fields, methods, and constructor arguments into the classes it generates.

These hidden fields and methods are often referred to as synthetic. You may want to use the javap disassembler to disassemble some of the class files for nested types so you can see what tricks the compiler has used to make the nested types work. See Chapter 13 for information on javap. The implementation of nested types works by having javac compile each nested type into a separate class file, which is actually a top-level class.

The compiled class files have a special naming convention, and have names that would not ordinarily be created from user code. Recall our first LinkedStack example Example , which defined a static member interface named Linkable.

When you compile this LinkedStack class, the compiler generates two class files. The first one is LinkedStack. This second class file contains the implementation of the static member interface defined in the exercise. Because the nested type is compiled into an ordinary top-level class, there is no way it can directly access the privileged members of its container. Therefore, if a static member type uses a private or other privileged member of its containing type, the compiler generates synthetic access methods with the default package access and converts the expressions that access the private members into expressions that invoke these specially generated methods.

The naming conventions for the four kinds of nested type are:. Because anonymous classes have no names, the names of the class files that represent them are an implementation detail. A local class is named according to a combination e.

Each instance of a nonstatic member class is associated with an instance of the enclosing class. This field is used to hold a reference to the enclosing instance. Every nonstatic member class constructor is given an extra parameter that initializes this field.

Every time a member class constructor is invoked, the compiler automatically passes a reference to the enclosing class for this extra parameter.

A local class is able to refer to fields and methods in its containing class for exactly the same reason that a nonstatic member class can; it is passed a hidden reference to the containing class in its constructor and saves that reference away in a private synthetic field added by the compiler. Like nonstatic member classes, local classes can use private fields and methods of their containing class because the compiler inserts any required accessor methods.

What makes local classes different from member classes is that they have the ability to refer to local variables in the scope that defines them.

The crucial restriction on this ability, however, is that local classes can reference only local variables and parameters that are declared final. The reason for this restriction becomes apparent in the implementation. A local class can use local variables because javac automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each local class constructor to initialize these automatically created private fields.

A local class does not actually access local variables but merely its own private copies of them. This could cause inconsistencies if the local variables could alter outside of the local class. One of the most eagerly anticated features of Java 8 was the introduction of lambda expressions. These allow small bits of code to be written inline as literals in a program and facilitate a more functional style of programming Java. In truth, many of these techniques had always been possible using nested types, via patterns like callbacks and handlers, but the syntax was always quite cumbersome, especially given the need to explicitly define a completely new type even when only needing to express a single line of code in the callback.

As we saw in Chapter 2 , the syntax for a lambda expression is to take a list of parameters the types of which are typically inferred , and to attach that to a method body, like this:. This can provide a very compact way to represent simple methods, and can largely obviate the need to use anonymous classes.

For example, consider the list method of the java. File class. This method lists the files in a directory. Before it returns the list, though, it passes the name of each file to a FilenameFilter object you must supply.

This FilenameFilter object accepts or rejects each file. For each file in the list, the block of code in the lambda expression is evaluated. If the method returns true which happens if the filename ends in. This pattern, where a block of code is used to test if an element of a container matches a condition, and to only return the elements that pass the condition, is called a filter idiom —and is one of the standard techniques of functional programming, which we will discuss in more depth presently.

When javac encounters a lambda expression, it interprets it as the body of a method with a specific signature—but which method? To resolve this question, javac looks at the surrounding code. To be legal Java code, the lambda expression must satisfy the following:. The expected interface method should have a signature that exactly matches that of the lambda expression. If this is the case, then an instance is created of a type that implements the expected interface, and uses the lambda body as the implementation for the mandatory method.

The lambda expression is said to be converted to an instance of the correct interface type. Some developers also like to use the term single abstract method or SAM type to refer to the interface type that the lambda is converted into. This draws attention to the fact that to be usable by the lambda expression mechanism, an interface must have only a single nondefault method.

Now, consider this lambda expression:. This will be autoconverted to an implementation of a FunctionalInterface that has a single nondefault method that takes a single MyObject and returns String. However, this seems like excessive boilerplate, and so Java 8 provides a syntax for making this easier to read and write:. This is a shorthand, known as a method reference , that uses an existing method as a lambda expression.

It can be thought of as using an existing method, but ignoring the name of the method, so it can be can used as a lambda, and autoconverted in the usual way. Java is fundamentally an object-oriented lanaguage. Current day processor is capable of decoding bit time. But what is the relation of this concept with the programming language JAVA? Let understand these as an example. Yes, we are going to use assembly language to get our code executed. We are going to give the command to a computer in this format, as shown below.

Your code to add two numbers in this language would be in this order. But how are we going to do this? Later, this code will be punched into the machine cards and feed to the computer. The computer will read these codes and execute the program. It converts it into the corresponding machine code But alone the assembler is not involved in this process; it also requires the compiler to compile the long code into a small chunk of codes.

It is used to convert your c language code into assembly code. The assembler converts it into corresponding machine code. This machine code will be transmitted to the processor. The most common processor used in PC or Computers are the Intel processor. Though present-day compilers come bundled with assembler can directly convert your higher language code into machine code.

The most common platform in the world is Windows, and Intel is called the Wintel Platform. As a developer, I want my software program to work on all platforms to maximize my revenues. So I would have to buy separate compilers that convert my print f command into the native machine code. But compilers come expensive, and there is a chance of compatibility issues.

So buying and installing a separate compiler for different OS and processor is not feasible. So, what can be an alternative solution? Enter Java language. By using Java Virtual Machine , this problem can be solved. But how it works on different processors and O. Step 1 The code to display the addition of two numbers is System.

Step 2 Using the java compiler the code is converted into an intermediate code called the bytecode. The output is a. Step 3 This code is not understood by any platform, but only a virtual platform called the Java Virtual Machine. When the Virtual Machine is fed with this bytecode, it identifies the platform it is working on and converts the bytecode into the native machine code.

While working on your PC or browsing the web, whenever you see either of these icons, be assured the java virtual machine is loaded into your RAM. But what makes Java lucrative is that code, once compiled, can run not only on all PC platforms but also on mobiles or other electronic gadgets supporting Java. Like the C compiler, the Java compiler does not produce native executable code for a particular machine.

Instead, Java produces a unique format called bytecode. It executes according to the rules laid out in the virtual machine specification. Therefore, Java is a platform-independent language. In short, the java source code can run on all operating systems. Skip to content.



0コメント

  • 1000 / 1000