Menu

Showing posts with label java8. Show all posts
Showing posts with label java8. 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 

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

Date and Time classes in Java

Today in this tutorial we will look how to work with date and times in Java programming.

Date and time is very important for every application and managing that date and time in your specified format, specified time zone and readable format as you want to get fetch from java program.

We are going to use some methods of LocalDate, LocalTime and LocalDateTime classes.

now() : This method return the current system date and time. If you call this now() using the LocalDate then it will give you date only not time stamp. But the same now if you call using the object of LocalTime then that will return time stamp only not date.

minusMonths() :  This method will reduce number of months from your given date. e.g. if your date is 2017-06-06 and when you will use minusMonths(2) then your date will become 2017-04-06.

plusMonths() : This method will add number of more months in your given date. e.g. if your date is 2017-04-06 and when you will use plusMonths(2) then your date will become 2017-06-06.

getDayOfWeek(): This method will return name of the day on a given time.

isBefore(): this getBefore method will return true or false by comparing the two given dates. e.g birthDate.isBefore(date.now()); here this will compare your birthdate is before current date or not. in the same way you can use the isAfter() as well.

Below is the complete example (ready to use) which you can just copy and paste  in your eclipse and execute to see how different Date methods are working in Java.


package abctest;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static java.time.temporal.TemporalAdjusters.*; // this import we have used to use next()import static java.time.DayOfWeek.*;
public class DateEx {
//here we have created three objects of LocalDate class. LocalDate now, bDate, today;
public static void main(String[] args) {
DateEx dateex=new DateEx();
// here we have created object local date(ld) from LocalDateTime.
LocalDateTime ld=LocalDateTime.now();
System.out.println(ld);
String locaDate=ld.format(DateTimeFormatter.ISO_DATE);
System.out.println(locaDate);
dateex.setNow(LocalDate.now());
dateex.dateMethods();
}
public void dateMethods(){
System.out.println("Date Methods:");
System.out.println("Date: " +getNow());
 //below line is to fetch the local time from your system.
System.out.println("Time: " +LocalTime.now());
//minusMonths() is to minus the number of months from a given date.
bDate=getNow().minusMonths(12);
//isBefore() method is used to compare the earlier date from two given dates.
System.out.println("Birthaday: "+bDate +" Is dbirthday before current date? "+bDate.isBefore(now));
//To get the name of the for a date.
System.out.println("He born on day: " +bDate.getDayOfWeek());
//On what date next Monday will be from a given date.
System.out.println("Next Monday will be on " +getNow().with(next(MONDAY)));
//From which era your date belongs from either BCE or AD.
System.out.println("Era: "+bDate.getEra());
}
public LocalDate getNow() {
return now;
}
public void setNow(LocalDate now) {
this.now = now;
}
}

Simple Spring project example

Create a implementation class ImplCode.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ImplCode {

 public static void main(String[] args) {

  ApplicationContext appContext=new FileSystemXmlApplicationContext("AppContextBeanFile.xml");

  Fruit fruit=appContext.getBean("fruit", Fruit.class);

  Vegetable veg=(Vegetable) appContext.getBean("veg");

  System.out.println(fruit.whoareyou());

  System.out.println(veg.whoareyou());

 }

}

Create a bean class Vegetable.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package rashiddemo;

public class Vegetable {

 public String whoareyou() {

  String intro="I am vegetable!!";

  return intro;

 }

}

Create another bean class Fruit.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package rashiddemo;

public class Fruit {

 public String whoareyou() {

  String intro="I am fruit!";

  return intro;

 }

}

Create a AppContextBeanFile.xml and add your bean classes using bean tag.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="fruit" class="rashiddemo.Fruit"></bean>

 <bean id="veg" class="rashiddemo.Vegitable"></bean>

</beans>