Tuesday, December 19, 2017

Java 8 Feature 1. Lambda Expressions


Lambda Expression:

You might have heard of lambda in mathematics.It is a bog change in math and is introduced in 1930’s.It solved complex problems easily.

Java is not first language to use lambda expression. First language which lambda expression is LISP.

There are other languages as well:
C, C++,objective c, c# .net, SCALA, ruby,python …. And Java.

Why Lambda ?

1. Lambda expression is to enable functional programming in Java.

2. To write more readable, maintainable and concise code.

3. To use APIs very easily and effectively.

4. To enable parallel processing.


There is wrong understanding in between developers that Lambda Expression has come to replace anonymous inner classes. Which is totally incorrect.








Basic difference between Anonymous Inner Class and Lambda Expression.



          Anonymous Class                                           lambda expression

  1. Class without name function without name

  1. Can extend abstract and can extend,can implement
Concrete class
    3.    Can implement interface with any          only 1 interface which contain single               abstract number of methods method (functional interface only)

4. We can declare instance variable can’t declare instance variable, whatever
Variable are considered as local variable.

5. Can be initiated can not

6. this always referer current inner    this always refers current outer class
Class object but not outer class object object

7. Best choice if want to handle multiple best choice to handle functional interface
  Methods.

8. For every class a separate .class file will no separate class
be generated internally

9. Memory will be allocated in heap inside method area. Inside permanent
Memory of JVM.




Demo Labmda Expression:

interface Interf {
public void m1();
}

public class Test1 {
int x=10;

public void m2(){

int y=20;

//lambda expression

Interf i = () -> {
System.out.println(x);
System.out.println(y);
};
i.m1();
}
public static void main(String[] args) {
Test1 t1 = new Test1();
t1.m2();
}
}



Any local variable which is referenced from lambda expression is always final whether we are declaring or not..In above sample code its y which is local and referenced from lambda expression.

This lambda expression is not executing inside the m2() method.But from t.m2()

You can not change local variable value which is y anywhere within or out of lambda expression.



If you are using local variable, then it becomes final.


Compile Error: Local variable y defined in an enclosing scope must be final or effectively final

If you are not using local variable in lambda expression, then its not final.




We can replace big code with small code and complexity using lambda expression.
Eg.

String s = “sks”;

Below highlighted is a function assigned to interface.  :)

Interf i = () -> System.out.println(“hello”);

We are in a position to pass lambda expression to the method as parameter and pass field in constructor.

Eg.

New Thread( () ->{
super(“child thread”);
}; )

Java 8 Feature and its purpose


Features:

Below mentioned are few features in java 8:

1. Lambda Expressions
2. Functional Interfaces   (type to call lambda expressions)
3. Default methods in interfaces (we can declare concrete methods here)  1.8 version onwards
4. Static methods in interfaces (also static inside interfaces) 1.8 version onwards
5. Predicate is  predefined functional interface
6. Function is predefined functional interface
7. Consumer is predefined functional interface
8. :: was in c++, but now in Java 8 with different purpose which is method reference and constructor reference.
9. Stream API :(Most Imp)  to perform bulk operation on collection becomes very easy. More readable and concise code in a single line instead of 6 lines.
10. Date and time API :  --  Introduced by joda.org-- Also known as Joda API


Purpose of java 8 features:

  1. To simplify programming.
  2. To enable/utilise functional programming benefits in Java in the form of Lambda expression.
  3. To enable parallel programming/processing in java.I can write more compatible code which is going to run on multi core systems. (overall performance is going to be improves).


Wednesday, September 20, 2017

fatal: Not a gitflow-enabled repo yet. Please run "git flow init" first.


fatal: Not a gitflow-enabled repo yet. Please run "git flow init" first.

Please follow below steps to remove this issue:


  1. Open the .git\config file
  2. Remove all the [gitflow * entries and save the file
  3. Close and re-open SourceTree
  4. Go in the main menu, go to Repository > Git Flow > Initialise Repository (should be enabled now)

Using Sed command to search and replace text in XML file or any other file .




Using Sed command to search and replace text in XML file or any other file .


To replace entire line with a specific text/string in linux by using sed command

Syntax:

sed -i 's/.*<TEXT>.*/<TEXT TO BE ON REPLACED LINE>/' *.xml

Replace below placeholders completely to put your required string/text in above syntax.

<TEXT>
<TEXT TO BE ON REPLACED LINE>

Please be aware of the escape sequence while executing the command. 


eg. Here I am replacing entire line which contains <lastmod> tag with <lastmod>2017-07-19T15:09:05+08:00</lastmod>

So please see that closing </lastmod> tag is having `\` (escape sequence). In you case it may vary depending upon your requirement.

sed -i 's/.*<lastmod>.*/<lastmod>2017-07-19T15:09:05+08:00<\/lastmod>/' *.xml

Be specific to the syntax and consider each and every comma and other preposition.









Thursday, June 22, 2017

Basic Java Interview questions.



What is the difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself. It has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.


What is the purpose of garbage collection in  Java, and when is it used?

The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.


What is an abstract class?

Abstract class must be extended/subclassed (to be useful). It served as a template. A class that is abstract may not be instantiated(you may not call its constructor), abstract class may contain static data.
Any class with abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.


What is the difference between an Interface and an abstract class?

An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An Interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected etc.), but has some abstract methods.


What are Checked and UnChecked Exception?

A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
Example: IOException thrown by java.io.FileInputStream’s read() method.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown.
Example: StringIndexOutOfBoundsException thrown by String’s charAt() method. Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.



Sunday, June 18, 2017

How to enable debugging in JSP in eclipse ? Or Can't setup debugging in Eclipse !





How to enable debugging in JSP in eclipse ? Or Can't setup debugging in Eclipse !

Go to Eclipse marketplace in eclipse and install JavaEE Developers tools.




After this, go to project properties and add the project which you want to debug and which is having the JSP's.




 Click Add and select the Project as shown below.



That's it, you can debug your application and start debugging you JSP file.






Thursday, June 15, 2017

How to download file by using shell script (sh) ? How to use wget in shell sctipt to download a file ?



How to download file by using shell script (sh) ? How to use wget in shell sctipt to download a file ?


-----------------------------------------download_file.sh---------------------------------
#!/bin/sh

# directory path where you want to download the file
DIR=/data/test

# wget output file with this name
FILE="test_$(date +'%Y%m%d').xml"

# wget  file download url
URL="http"//www.xyz.com...... "

cd $DIR

#wgt syntax to download file

wget  $URL -O  $FILE



Saturday, June 3, 2017

Warning: mysql_connect(): No such file or directory in /Library/WebServer/Documents/





Warning: mysql_connect(): No such file or directory in /Library/WebServer/Documents/<YOUR_FILE_NAME.php>


You might need to change below files in you Mac OS.

If this file exist in your machine /tmp/mysql.sock but not /var/mysql/mysql.sock then do the perform below steps:

cd /var 
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /tmp/mysql.sock mysql.sock


But if  /var/mysql/mysql.sock exist in your machine but not  /tmp/mysql.sock this file, then do the below steps :...

cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock



Good Luck !!!
Like, comment and share the knowledge....!!!

Wednesday, May 31, 2017

How to download file/folder from Linux Server to Mac's Desktop or any location ?






Download a directory from the Linux Server

scp -r <user_name>@<host_ip_address>:<PATH_OF_DIRECTORY_TO_DOWNLOAD>  <PATH_ON_LOCAL_DESKTOP>

Enter Linux Server Password:




Download a file from the Linux Server

scp <user_name>@<host_ip_address>:<PATH_OF_FILE_TO_DOWNLOAD>  <PATH_ON_LOCAL_DESKTOP>

Enter Linux Server Password:

Thursday, April 27, 2017

How to make Apache Tomcat as service in Windows 7 ?



How to make Apache Tomcat as service in Windows 7 ?

Go to the tomcat location c:\apache-tomcat-7.0.77\bin and write command service.bat install as
show in the below picture.





2. You will see the 'Tomcat X' has been started.

Note: X is the version of your Tomcat.






3. Monitor your installed Tomcat service.

c:\apache-tomcat-7.0.77\bin>tomcat7w.exe //MS//Tomcat7




You will see below dialog,






How to set+modify Java option ? or How to set+modify PATH system variable ?

How to set/modify the PATH system variable?


This post applies following OS's :

Windows 7, Windows 8, Windows Vista, Windows XP

Macintosh OS X, 

Oracle Linux, 

Red Hat Linux, 

SUSE Linux, 

Solaris SPARC,




Information for All

  • The PATH is a system variable that your operating system uses to locate needed executables from the command line or Terminal window.

  • The PATH system variable can be set using System Utility in control panel on Windows, or in your shell's startup file on Linux and Solaris.

  • Making changes to the system PATH variable is typically not necessary for computers running Windows or Mac OS X.

Windows

Windows 10 and Windows 8

  1. In Search, find for and then select: System (Control Panel)
  2. Click the Advanced system settings link.
  3. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  4. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  5. Reopen Command prompt window, and run your java code.
Windows 7

  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  6. Reopen Command prompt window, and run your java code.
Windows Vista

  1. From the desktop, right click the My Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced tab (Advanced system settings link in Vista).
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  6. Reopen Command prompt window, and run your java code.
Windows XP

  1. Select Start, select Control Panel. double click System, and select the Advanced tab.
  2. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  3. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  4. Reopen Command prompt window, and run your java code.

Mac OS X

To run a different version of Java, either specify the full path, or use the java_home tool:
% /usr/libexec/java_home -v 1.8.0_73 --exec javac -version

Solaris and Linux

  1. To find out if the path is properly set:
    In a terminal windows, enter:
    % java -version
    This will print the version of the java tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.
  2. Determine which java executable is the first one found in your PATH
    In a terminal window, enter:
    % which java
Set the PATH permanently

To set the path permanently, set the path in your startup file.
Note: Instructions for two most popular Shells on Linux and Solaris are listed. If you are using other shells, see the Path Setting Tutorial.
Bash Shell
Edit the startup file (~/.bashrc)
  1. Modify PATH variable
    PATH=/usr/local/jdk1.8.0/bin:$PATH
    export PATH
  2. Save and close the file
  3. Load the startup file
    % . /.profile
  4. Verify that the path is set by repeating the java command
    % java -version
C Shell (csh)
Edit the startup file (~/.cshrc)

  1. Set Path
    set path=(/usr/local/jdk1.8.0/bin $path)
  2. Save and close the file
  3. Load the startup file
    % source ~/.cshrc
  4. Verify that the path is set by repeating the java command
    % java -version

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...