Showing posts sorted by relevance for query jdbc-difference-between. Sort by date Show all posts
Showing posts sorted by relevance for query jdbc-difference-between. Sort by date Show all posts

Tuesday, December 10, 2019

Difference Betwixt Public, Soul Together With Protected Modifier Inwards Java?

In Java, you lot conduct maintain got something called access modifier, which specifies accessibility of class, methods too variables. There is iv access modifier inwards Java namely public, private, protected too the default access modifier, likewise known equally package grade modifier. The departure betwixt these access modifier comes inwards their powerfulness to throttle access to a class, method or variables, public is the to the lowest degree restrictive access modifier spell person is the most restrictive access modifier, package too protected lies inwards between. Another cardinal difference betwixt public, protected, packet too person modifier come upward from the indicate where you lot tin apply them, for example, you lot cannot purpose private or protected modifier alongside a transcend grade class but you lot tin purpose populace modifier there.

The default or packet modifier is picayune flake special, when you lot create non specify whatever of the public, protected too person modifier, which is likewise the keywords, too then Java automatically apply a default modifier (no it doesn't purpose default keyword), which agency the said class, method or fellow member volition alone live accessible inside the package it has declared.

Any class, which is exterior the said packet cannot access that element. The expert matter virtually these concepts is that departure betwixt public, protected too person inwards Java is likewise ane of the frequently asked Java interview questions.




Difference betwixt public, packet too person modifier

Apart from the fact that each access modifier allows members a specific grade of access, at that spot is or thence to a greater extent than subtle departure betwixt them e.g. where precisely tin you lot purpose them. Let's detect out to a greater extent than virtually unlike access modifiers inwards Java.

1. One of the most of import departure you lot demand to shout back related to access modifier is the grade of accessibility e.g. anything populace is accessible to anywhere too anything person is alone accessible the class they are declared. Similarly, anything default is accessible alone within the packet too anything protected is accessible exterior the packet but alone to tyke classes.

This means, you lot tin access populace class, method or variable from anywhere inwards Java computer program too that's why cardinal API are ever populace e.g. List interface, ArrayList class etc but you lot powerfulness desire to shroud their implementation too you lot tin create that past times using person access modifier.

Why create you lot desire to shroud implementation? thence that you lot tin alter it afterwards without whatever worry that it volition interruption whatever client. One of the best examples of hiding implementation is RegularEnumSet too JumboEnumSet classes which implement EnumSet interface.

EnumSet is populace too these ii classes are hidden at packet level, which allows EnumSet to conduct whatever of these depending upon cardinal size of Enum. If the enum whose constants you lot are storing inwards EnumSet has less than 64 too then it chooses RegularEnumSet which is to a greater extent than efficient.

If you lot are looking for a Java evolution seat too preparing for ane too then you lot tin likewise accept assist from the wonderful book Java Interview Exposed past times Wrox. It is ane of the rare consummate guides for a Java developer too tells which topic is of import from interview indicate of view.

 you lot conduct maintain got something called access modifier Difference betwixt Public, Private too Protected modifier inwards Java?


2. Second departure betwixt populace too modifier is that they supply Encapsulation, which is simply contrary of accessibility. Encapsulation says shroud anything which varies too that's why nosotros shroud implementations, populace keyword provides lowest grade of Encapsulation too person modifier provides highest grade of Encapsulation inwards Java.


3. Third departure betwixt person too populace modifier is that you lot tin purpose populace modifier alongside transcend grade class but you lot cannot brand a transcend grade class person inwards Java. Though both populace too person modifier tin live used alongside class e.g. nested too inner classes. You tin brand an inner class person which agency no ane tin access it exterior enclosing class.

Here is a overnice tabular array which compares access grade provided past times public, protected, person too default access modifier inwards Java.

 you lot conduct maintain got something called access modifier Difference betwixt Public, Private too Protected modifier inwards Java?











As I said, purpose populace modifier to define API too purpose person modifier to shroud implementation, but you lot tin likewise brand constants populace e.g. class variable which is likewise marked lastly too static.




Difference betwixt default too protected modifier inwards Java

So, instantly you lot know the departure betwixt person too populace modifier inwards Java too empathise when to purpose populace too person inwards Java. Now, its fourth dimension to empathise departure betwixt default or packet grade access too protected modifier inwards Java.

1) First difference betwixt default too protected access modifier inwards Java is that default is nil but packet grade accessibility i.e. if you lot don't supply whatever access modifier to a class, method or variable too then Java past times default brand them accessible within the package. If you lot cannot brand a fellow member person too then your side past times side best choice is package grade access. That's why its likewise called package-private access. On the other hand, protected modifier provides to a greater extent than accessibility too then default modifier. You tin access a protected fellow member exterior the package, but alone within sub classes.

For event if a method getVersion() is protected within a class too they are within build packet too class is populace too then this method is accessible to all classes within cook packet but alone to sub classes exterior cook package.

package build; public class Builder{  protected int getVersion(){    return 1; }  }

too hither is our sub class :

package gui; public class Display extends Builder {  public void show(){    System.out.println(getVersion()); //Ok, protected method tin live called from subclass exterior package }  }

You tin run across that nosotros tin access getVersion() method which is protected, within or thence other packet gui without whatever problem.


That's all virtually difference betwixt public, person too protected modifier inwards Java. Rule of pollex is to continue things equally much person equally possible because that allows you lot flexibility to alter them later. If you lot cannot brand them person too then at-least continue them package-private, which is likewise the default access grade for class, methods too variables inwards Java. You should likewise continue inwards hear that access modifier tin alone live applied to members e.g. variables too methods which are business office of class, you lot tin non brand a local variable public, person or protected inwards Java.

If you lot similar give-and-take inwards this interview questions, you lot would likewise similar my explanation of other oft asked Java interview questions equally shown below :
  • Can nosotros override person method inwards Java? (answer)
  • Can nosotros override person method within inner class inwards Java? (answer)
  • What is departure betwixt overloading too overriding inwards Java? (answer)
  • Difference betwixt type-1 too type-2 JDBC driver inwards Java? (answer)
  • Difference betwixt == too equals() inwards Java? (answer)
  • What is departure betwixt String too StringBuffer inwards Java? (answer)

Saturday, November 9, 2019

How To Compare 2 Dates Inwards Java? Banking Corporation Gibe If They Are Equal, Afterward Or Earlier

There are multiple ways to compare ii dates inwards Java, it too depends on upon what exactly comparing means. If y'all are looking to compare ii dates to abide by out whether they are equal or not, thence y'all tin just utilisation equals() method of java.util.Date class. This method volition render truthful if both dates are equal, just both thave same millisecond value. If y'all are looking to abide by out whether a engagement comes earlier or afterward about other engagement thence y'all receive got 3 choices, y'all tin utilisation compareTo() method of java.util.Date class, or y'all tin utilisation before() in addition to after() method of Date class, or y'all tin utilisation before() in addition to after() method of Calendar class. I propose leveraging before() in addition to after() method of Date class, they are to a greater extent than readable in addition to tardily to use.

BTW, all these methods compare engagement at the millisecond level, which agency they accept both engagement in addition to fourth dimension into account. If y'all desire to compare just engagement purpose without considering time, y'all bespeak to utilisation DateFormat shape to format engagement into about format in addition to thence compare their String value.

Alternatively, y'all tin utilisation joda-time which provides a shape LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If y'all are running inwards Java 8, thence at that spot is no argue non to utilisation novel Date in addition to Time API.  If y'all are inwards a hurry, thence come across Java SE 8 for Really Impatient by Cay S. Horstmann to larn how to utilisation novel Date in addition to Time API of Java 8 inwards your onetime in addition to novel Java application.



How to depository fiscal establishment jibe if ii dates are equal inwards Java

There are ii ways to depository fiscal establishment jibe if ii dates are equal inwards Java :
  • Date's equals() method - render truthful if ii dates are equal
  • Date's compareTo() method - render nil if ii dates are equal



If y'all are doing equality depository fiscal establishment jibe thence it brand feel to utilisation equals() method. It does comparing past times checking millisecond values of given dates every bit shown below :

public boolean equals(Object obj) {    return obj instanceof Date && getTime() == ((Date) obj).getTime(); }

So, if ii dates receive got same millisecond they are considered equal. See Java How to Program past times Dietel for to a greater extent than examples.


How to depository fiscal establishment jibe if a engagement comes earlier or afterward another

There are 3 ways to depository fiscal establishment jibe if i engagement comes earlier or afterward about other engagement inwards JDK, without using whatever tertiary political party library similar joda.

1) Date's before() in addition to after() method - volition render truthful or false
2) Calendar's before() in addition to after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement representative is less than given engagement representative in addition to +1 if this engagement representative is greater than given engagement instance.

All these methods perform a comparing based upon millisecond values only, every bit shown inwards below code of before() in addition to after() methods of java.util.Date shape :

public boolean before(Date when) {    return getMillisOf(this) < getMillisOf(when); }  public boolean after(Date when) {    return getMillisOf(this) > getMillisOf(when); }

See Core Java, Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than almost comparing Dates inwards Java.

















Java Program to compare Dates inwards Java

package dto;  import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;  /**  * 2 ways to compare given dates inwards Java without using  * whatever tertiary political party library e.g. joda time.  *   * @author WINDOWS 8  *  */ public class DateComparator {      public static void main(String args[]) {        Calendar todayCalendar = new GregorianCalendar(2015, Calendar.AUGUST, 24);        Calendar yesterdayCalendar = new GregorianCalendar(2015, Calendar.AUGUST, 23);                Date today = todayCalendar.getTime();        Date yesterday = yesterdayCalendar.getTime();                // comparing dates using equals() method inwards Java        // today in addition to yesterday is non equal        if(today.equals(yesterday)){            System.out.println("two given dates are equal to each other");        }else{            System.out.println("two dates are non equal to each other ");        }                        // y'all tin too depository fiscal establishment jibe if ii dates are equal or non using        // compareTo() method, java.util.Date implements         // Comparable in addition to its compareTo() is consistent amongst equals()                  if(yesterday.compareTo(today) == 0){            System.out.println("Given dates are same");        }else{            System.out.println("Given dates are dissimilar ");        }                        // checking which dates comes start         // y'all tin utilisation before() in addition to after() method of Calendar class        if(todayCalendar.after(yesterdayCalendar)){            System.out.println("first engagement comes afterward mo date");        }                // Date shape too has their ain before() in addition to after() method        if(yesterday.before(today)){            System.out.println("yesterday comes earlier today");        }     }       }  Output ii dates are not equal to each other  Given dates are dissimilar  the first engagement comes afterward the mo engagement yesterday comes earlier today


That's all almost how to compare ii dates inwards Java. When y'all compare dates, it takes both engagement in addition to fourth dimension into consideration, because it compares on long millisecond value. Also scream back that Date shape doesn't comprise whatever timezone information. Date concord let out of millisecond passed from 1st Jan 1970 inwards UTC. If y'all desire to compare dates without taking fourth dimension into consideration, take in using Java 8 novel Date in addition to Time API or joda-time, if y'all are running on Java SE 5, half dozen or 7.

Related Java Date in addition to Time tutorial for Programmers
  • How to convert java.sql.Date to java.util.Date inwards Java? (example)
  • How to acquire the electrical current Timestamp inwards Java? (answer)
  • Difference between java.util.Date in addition to java.sql.Timestamp inwards JDBC? (answer)
  • How to format Date inwards Java using SimpleDateFormat? (example)
  • How to convert String to Date inwards Java? (answer)
  • How to display Date inwards multiple timezones inwards Java? (example)
  • How to convert java.util.Date to java.sql.Date inwards JDBC? (answer)
  • How to convert java.util.Date to java.sql.Timestamp inwards JDBC? (example)
  • How to parse String to Date inwards Java using JodaTime library? (example)


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!