Notes for programming – mixed – Q & A – Part 1
1.) What is difference between extends and implementations in java (20/09/2016) ?
1.1.a) extends is for extending a class.
1.1.b) implements is for implementing an interface
Difference Between Implements and Extends
2.) which is the best practice for checking condition if the Object is null (21/09/2016)?
– Lets say you want to checking if your object input are null, there are three ways:
1) Add null first to check the input
CORRECT – The best practice
2) Add input first then check with null
INCORECT – not the best practice , WHY ? – it will potentially causing a NullPointerException.
3) check the condition by == this syntax equals
if(input == null)
CORRECT – The best practice
3.) what is best practice for Java String method (local) variables be initialized to null or “”?
- its depends on what your programs want to do, there are certain time we need to initialized or directly set to null because we want to check the condition input != null and do something, but if we wanted to prevent the nullpointer its better to set “” – EMPTY string.
4.) Missing braces in If statement is not recommendation.
4.1. First codes without curly braces:
4.2. Second codes with curly braces:
- if the First and the second is the best if-else recommendation.
- answers: when not Omitting braces it will harms readability and can easily cause issues when the code is modified.
- for the first if statement , the compiler will read for the first do this.
- Code merges. It’s been known to happen on projects I’ve worked on that single-statement ifs have been broken by automatic merges. The scary thing is the indentation looks right even though the code is wrong, so this type of bug is hard to spot.So I go with braces – on their own lines. It’s easier to spot the levels that way. Yes, it does waste vertical screen real estate and that’s a genuine downside. On balance though, I think it’s worth it.
- if really don’t want to use curly braces, write just one line only, its really safeful:
5.) Date date = new Date(); date.parse(“String_date_format“) , does no longer supported, Deprecated. .parse() :
- With Java 8, the preferred date/time classes are no longer in the java.util package and the preferred date/time handling classes are now in the java.time package.
- The DateTimeFormatter class provides of Pattern methods to provide an instance of DateTimeFormatter based on the provided date/time pattern String. One of the format methods can then be called on that instance of DateTimeFormatter to get the date and/or time information formatted as a String matching the provided pattern.
java.util.Date date = new java.util.Date();
java.text.SimpleDateFormat dateformat = new java.text.SimpleDateFormat(“YYYY-dd-MM”);
date.parse(date.parse()); // The method parse(String) from the type Date is deprecated
5.2) NEW Date Format for Java 8
– using: java.time.LocaleDate();
LocalDate date = LocalDate.now();
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);
5.3.) How to passing value Date with Format Pattern – “YYYY-dd-MM” to save to database? (29/09/2016 – 11.00 am – HTV Building)
LocalDate localDate = LocalDate.now(); // YYYY-mm-dd - java.time 8
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // java.time 8
SimpleDateFormat smpdatef = new SimpleDateFormat("yyyy-MM-dd"); // java.text
String strldate = localDate.format(formatter);
Date date = smpdatef.parse(strldate);
// using sql date to change to Date
java.sql.Date datesql = null;
datesql = new java.sql.Date(smpdatef.parse(strldate).getTime());
System.out.println("date sql : " + datesql);
// set to entity with Date value
Users user = new Users();
Output: date sql : 2016-09-29
6.) Operator: what is super() method in java? (4/10/2016)
1.1. Super keyword are to use for accessing Superclass’s Member and reference variable is used to refer parent class Object.
1.2. the Subclass method overrides one of its superclass’s methods, it can invoke the overridden method through the use for keyword super.
a. At variable level.
b. At method level.
c. At constructor level.