Menu

Showing posts with label Java9. Show all posts
Showing posts with label Java9. Show all posts

Pass by reference and pass by value in Java

Today in this tutorial of Java program we will see how to work with pass by value and pass by reference. 

Below is code snippet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

package com.rashid.jorvee;


public class ValueRef {

 public static void main(String[] args) {

  Employee emp=new Employee();

  ValueRef valref=new ValueRef();

  emp.setSalary(12000.00);

  System.out.println("Employee emp salary: " +emp.getSalary());

  valref.salary(emp);

 }

 public void salary(Employee em){

  em.setSalary(14000.00);

  em=new Employee();

  em.setSalary(15000.0);

  System.out.println("Employee em salary: " +em.getSalary());

 }

}



class Employee{

 double salary;

 public double getSalary() {

  return salary;

 }


 public void setSalary(double salary) {

  this.salary = salary;

 }
}

Output:
Employee emp salary: 12000.0
Employee em salary: 15000.0 

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

If you are getting error "No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?" while compiling any java application either using Eclipse, NetBeans and any other IDE, the simple meaning of this error is; you are trying to compile your Java program using JRE which is not possible. To compile any Java program we need JDK.

There could be two reasons which causing this error. Will see both the ways to fix this issue.

How to check installed version of Java in your machine.

1. If you don't have JDK installed in your machine/Computer.

  • To resolve this you need to download the latest version of JDK and install it in your machine. 
  • Then setup environment  variable and path. for more check here.


2. If JDK is already installed in your machine but installed JRE path is not setup to JDK path. 

  • Open eclipse
  • Go to Window > Preferences > Java > Installed JREs > and check your installed JREs.
  • There should be a entry available for JDK in installed JRE. In case there is no JDK entry then you have to add path of installed JDK from your machine.

To do this:
  • Go to C drive of your computer and find the directory of JDK in program files. e. g. C:\Program Files\Java\jdk1.8.0_111 
  • Copy this path and add this path in your installed JREs option in your IDE.
inatalled JRE in eclipse
Inatalled JRE in eclipse
  •  Click on Add button from right side of window. A new window will appear, click on standard VM > Next > In JRE home section put the path of installed JDK from program files and click on finish.
JRE defination window
JRE defination window

  • After doing these all JDK will be appear in your installed JREs window. From there you mark/check JDK and apply.
inatalled JRE in eclipse
Inatalled JRE in eclipse

    How to check installed Java version in your machine?

    If you see this error while executing any program or running and debugging maven build then follow the following easy steps, to resolve this issue from eclipse.


    How to check installed Java version in your machine?

    There are two ways to check installed java version in your machine.
    1. Using command prompt
    • Go to start and search for cmd, open command prompt.
    • No need to change the default directory from command prompt, just  copy and paste command "java -version".
    • Hit enter key to run the command.

    Below is the screen shot for more clarification.


    how to check java version
    Java version check

    2. Another simple way to check installed java version in your machine by using java version link.


    Verify version of java installed in your machine
    Verify Java and find out-dated version



    • Click on the Agree and continue button.
    • A popup window will appear, which needs your approval to run java version check application. Simply click on the "Run" button on that popup.
    • Now you will see the Java detail page on your browser. as showing in the below screen shot.

    Java version in your machine
    Java version in your computer

    New features in java language in Java-9 or JDK-9 release

    The major change in Java SE 9 is its module system. Other than this there are several small language changes as well in Java SE 9.

    As we already know on 21st Sept 2017 JDK 9 or Java 9 has been release in market. Today we will see what all new programming features introduced in Java SE-9.


    • You can now declare a private method in a Interface class. This allows non-abstract methods of an interface to share code between them. 
    For Example: 
    public interface AccountOp{
    public static final String NAME="wellDo";

    //default method of interface
    public default void foot(){}
    //private method within interface
    private void displayAge();
    }


    • Underscore character no more legal for identifier name. Your source code will not compile if you use underscore character ["_"] in an identifier name[variables, methods, classes, packages and interfaces]. e.g. String ras_name; interface Local_Home; public void gender_Test(); are the illegal in Java 9/JDK 9.


    • Allow effectively final variables to be used as resources in the try-with-resources statement. If you already have a resource as a final or effectively final variable, you can use that variable in a try-with-resources statement without declaring a new variable. An "effectively final" variable is one whose value is never changed after it is initialized.
      For example, you declared these two resources:
                // A final resource

                final Resource resource1 = new Resource("resource1");
                // An effectively final resource
                Resource resource2 = new Resource("resource2");
                In Java SE 7 or 8, you would declare new variables, like this:
                          try (Resource r1 = resource1;

                               Resource r2 = resource2) {
                              ...
                          }
                          In Java SE 9, you don’t need to declare r1 and r2:
                            // New and improved try-with-resources statement in Java SE 9

                                    try (resource1;
                                         resource2) {
                                        ...
                                    }


                            • @SafeVarargs annotation is allowed on private instance methods.

                            The @SafeVarargs annotation can be applied only to methods that cannot be overridden. These include static methods, final instance methods, and, new in Java SE 9, private instance methods.


                            • You can use diamond syntax in conjunction with anonymous inner classes.
                            Types that can be written in a Java program, such as int or String, are called denotable types. The compiler-internal types that cannot be written in a Java program are called non-denotable types.
                            Non-denotable types can occur as the result of the inference used by the diamond operator. Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute in class files, using the diamond with anonymous classes was not allowed in Java SE 7.
                            In Java SE 9, as long as the inferred type is denotable, you can use the diamond operator when you create an anonymous inner class.

                            For more detail information please refer the Java official doc.


                            Reference:

                            1. https://docs.oracle.com/javase/9/language/toc.htm
                            2. http://www.oracle.com/technetwork/java/javase/9all-relnotes-3704433.html
                            3. https://docs.oracle.com/javase/9/whatsnew/toc.htm

                            Key Changes in Java 9

                            On 21st September 2017 Oracle released new version of Java/JDK version 9.

                            in this tutorial we will see what all are the key changes in Java 9 and how it will improve the performance of an application and who this things are going to help a programmer to write good programs.

                            Java Platform Module System

                            Introduces a new kind of Java programming component, the module, which is a named, self-describing collection of code and data. This module system have following features:

                            1. Introduces the modular JAR file, which is a JAR file with a module-info.class file in its root directory.
                            2. Introduces a new optional phase, link time, which is in-between compile time and run time, during which a set of modules can be assembled and optimized into a custom runtime image.
                            3. Adds options to the tools javac, and java where you can specify module paths, which locate definitions of modules.
                            4. Introduces the JMOD format, which is a packaging format similar to JAR except it can include native code and configuration files.
                            5. The JDK itself has been divided into a set of modules. 

                            For more detail information you can download Java 9 release notes from here.

                            New Version-String Scheme

                            Provides a simplified version-string format that helps to clearly distinguish major, minor, security, and patch update releases.


                            The new version-string format is as follows: 

                            $MAJOR.$MINOR.$SECURITY.$PATCH

                            For more detail see version-string format or browse the link from reference.

                            References: