Сообщения

Сообщения за март, 2021

Date, SimpleDateFormat

package com.main ; import java.text.SimpleDateFormat ; import java.util.Date ; public class Main { public static void main ( String [] args ) throws Exception { String dateStr = "2021-03-29 18:29:02" ; SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ) ; Date date = sdf .parse( dateStr ) ; System . out .println( date .getTime()) ; Date d = new Date( 1617028142000l ) ; System . out .println( d ) ; SimpleDateFormat sdf2 = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ) ; System . out .println( sdf2 .format( d )) ; Date now = new Date() ; System . out .println( now ) ; } }

Mutable and Immutable classes

 Let's see this example: package com.main ; public class Mercedes { private int speed ; public Mercedes () { } public Mercedes ( int speed ) { this . speed = speed ; } public int getSpeed () { return speed ; } public void setSpeed ( int speed ) { this . speed = speed ; } } package com.main ; public class BMW { public Integer speed ; private String color ; private Mercedes mercedes ; public BMW () { } public BMW ( Integer speed ) { this . speed = speed ; } public BMW ( Mercedes mercedes ) { this . mercedes = mercedes ; } public Integer getSpeed () { System . out .println( "BMW class" ) ; return speed ; } public String getColor () { return color ; } public Mercedes getMercedes () { Mercedes m = new Mercedes() ; m .setSpeed( mercedes .getSpeed()) ; return m ; } } package com.main ; public class

isEmpty(); and isBlank();

  package com.company ; public class Main { public static void main ( String [] args ) { String a = " " ; System . out .println( a .isEmpty()) ; System . out .println( a .isBlank()) ; String b = "" ; System . out .println( b .isEmpty()) ; System . out .println( b .isBlank()) ; } } Outcome: false true true true a - is not empty despite the fact that there is a space inside it. Any character inside string means it is not empty. a - is blank, because it does not have any character and has only space inside it. b - is empty, because it does not have any character. That is why it is true. b - is blank, because it does not have any character.

Design Pattern - Proxy Pattern

package com.proxy ; public interface OfficeInternetAccess { public void grantInternetAccess () ; } package com.proxy ; public class RealInternetAccess implements OfficeInternetAccess { private String employeeName ; public RealInternetAccess ( String employeeName ){ this . employeeName = employeeName ; } @Override public void grantInternetAccess () { System . out .println( "Internet Access granted for employee: " + employeeName ) ; } } package com.proxy ; public class ProxyInternetAccess implements OfficeInternetAccess { private String employeeName ; private RealInternetAccess realaccess ; public ProxyInternetAccess ( String employeeName ){ this . employeeName = employeeName ; } @Override public void grantInternetAccess () { if (getRole( employeeName ) > 4 ){ realaccess = new RealInternetAccess( employeeName ) ; realaccess .grantInternetAccess() ; } else {

Design Pattern - Factory Pattern

  Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. package com.company ; public interface Shape { void draw () ; } package com.company ; public class Rectangle implements Shape { @Override public void draw () { System . out .println( "Rectangle" ) ; } } package com.company ; public class Square implements Shape { @Override public void draw () { System . out .println( "Square" ) ; } } package com.company ; public class Circle implements Shape { @Override public void draw () { System . out .println( "Circle" ) ; } } package com.company ; public class ShapeFactory { public Shape getShape ( String shapeType ){

Server ve Servlet. Redirect ve Forward.

 Server dediyimiz anlam bir daima aciq olan komputerdir. Ve her bir komputerin tekrarsiz bir IP(Internet  Protocol). Lakin biz her hansisa sayta daxil olanda biz tebiiki IP adres yazmiriq, biz domek daxil edirik (mes. google.com, facebook.com ve s.) . Tutaq ki, biz bir sayt acmishiq ve bu sayt mes: 184.1.6.1 ip-li bir komputerde yerleshir. Sonra bir domen aliriq ve bu domeni baglayiriq bu ip adrese. Mes. shopping.com bu sayt baglanir hemin ip adrese. Ne zaman shopping.com sorgu gelse bu sorgu yoneldilir hemin ip adrese. Demek ki, bizim servere sorgu bu shekilde gelib cixir. Sorgu gelib cixandan sonra gelib dushur bizim tomcat servere. Tomcat oz novbesinde bu url uygun servlet ishledir. Niye gore Tomcat-a gelib cixir? Cunki, hemin ip adresde (Daha deqiq desek mes: 184.1.6.1:80) yeni komputerin 80 portunda Tomcat oturur ona gorede requesti handle edir ve uygun servlet-e verir.

JSP - Java Server Page

 user.jsp sehifesi <%@ page contentType =" text/html;charset=UTF-8 " language =" java " %> <html> <head> <title> User </title> </head> <body> <form action ="UserController" method ="post" > <input type ="text" name ="alma" value ="Parvin" > <input type ="text" name ="armud" value ="Etibarli" > <input type ="submit" name ="heyva" value ="SAVE" > </form> </body> </html> UserController.java sehifesi package com.controller ; import javax.servlet.ServletException ; import javax.servlet.annotation. WebServlet ; import javax.servlet.http.HttpServlet ; import javax.servlet.http.HttpServletRequest ; import javax.servlet.http.HttpServletResponse ; import java.io.IOException ; @WebServlet (name = "UserController" , urlPatterns = "/UserController" ) pu

Exceptions

  An exception (or exceptional event) is a problem that arises during the execution of a program. When an  Exception  occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. package com.main ; public class Main { public static void main ( String [] args ) { try { int a = 5 ; int b = 0 ; int k = a / b ; System . out .println( "output is :" + k ) ; } catch ( Exception ex ){ System . err .println( "Error " ) ; } System . out .println( "continue..." ) ; } } Output: Error continue... If an error(exception) occurs java skips the rest of the code and jumps directly to the catch block and then the rest of the code arter catch block. package com.main ; public class Main { public static void main ( String [] args ) { try { int a = 5 ;

Interface

 Like a normal class, an Interface can have methods and variables, but the methods declared in an interface  are by default abstract (only method signature(head), no body). *Interfaces specify what a class must do and not how. It is a blueprint of the class. So it specifies a set of methods that the class has to implement. *If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.  *Interface abstract methods can not have body. *By default methods in Interface class have (public abstract) keyword, but there is no need to type them. For example: public abstact void start(); it is the same as void start();  By default there is always public abstact keyword! package com.company ; public interface Startable { public abstract void start () ; // public abstract is by default, and there is no need to write them } package com.company ; public class BMW { } package com.company ; public class

Inner and Nested classes

  package com.company.bean ; public class Kia { public static class A { // nested class or static class public static void foo () { System . out .println( "A" ) ; } } public class B { // inner class or instance class public void foo () { System . out .println( "B" ) ; } } } package com.company ; import com.company.bean.Kia ; public class Main { public static void main ( String [] args ) { Kia . A a = new Kia .A() ; Kia . B b = new Kia(). new B() ; a . foo () ; b .foo() ; Kia . A . foo () ; System . out .println( a .getClass().getName()) ; System . out .println( b .getClass().getName()) ; } } Outcome: A B A com.company.bean.Kia$A com.company.bean.Kia$B As we can see nested classes must be static, however inner classes instance. And another important fact is there are anonymous(inner classes after compiling.

Anonymous classes

  It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class. package com.company.bean ; public class A { public void show (){ System . out .println( "class A" ) ; } } package com.company.bean ; public class B extends A { public void show (){ System . out .println( "class B" ) ; } } package com.company ; import com.company.bean.A ; public class Main { public static void main ( String [] args ) { A obj = new A (){ public void show (){ System . out .println( "mutant" ) ; } } ; obj .show() ; System . out .println( obj .getClass().getName()) ; } } Outcome: mutant com.company.Main$1 *It is also possible for Interface and Abstract clesse

Abstract classes

  One of the fundamental concepts in OOP is the abstract class. Abstract classes cannot be instantiated and are designed to be subclassed. They are used to provide some common functionality across a set of related classes while also allowing default method implementations. Rules to Remember Abstract classes cannot be instantiated. If a class has at least one abstract method, then the class must be declared abstract. To use an abstract class, we must create a class that extends the abstract class (inheritance) and provide implementations for all abstract methods. Java does not support multiple inheritance so we are only allowed to extend one class (abstract or not). There is where interfaces become useful. package com.company.bean ; public abstract class Car { public abstract void start () ; public abstract void stop () ; } package com.company.bean ; public abstract class BMW extends Car { @Override public void start (){ System . out .println( "BMW start&quo