Use Strings carefully β οΈ
If two Strings are concatenated using β+β operator in a βforβ loop, then it creates a new String Object, every time.
This causes wastage of Memory and increases Performance time.
Also, while instantiating a String Object, constructors should be avoided and instantiation should happen directly. For example:
#memory #java #string
@ProgrammingTip
If two Strings are concatenated using β+β operator in a βforβ loop, then it creates a new String Object, every time.
This causes wastage of Memory and increases Performance time.
Also, while instantiating a String Object, constructors should be avoided and instantiation should happen directly. For example:
//Slower Instantiation β
String bad = new String("string object");
//Faster Instantiation β
String good = "string object";
#memory #java #string
@ProgrammingTip
Avoiding Memory Leaks By Simple Tricks β
Memory Leaks often cause performance degradation of software. Since, Java manages memory automatically, the developers do not have much control.
But there are still some standard practices which can be used to protect from memory leakages. ππ»ββοΈ
1οΈβ£ - Always release database connections when querying is complete.
2οΈβ£ - Try to use Finally block as often possible.
3οΈβ£ - Release instances stored in Static Tables.
#java #memory #performance
@programmingTip
Memory Leaks often cause performance degradation of software. Since, Java manages memory automatically, the developers do not have much control.
But there are still some standard practices which can be used to protect from memory leakages. ππ»ββοΈ
1οΈβ£ - Always release database connections when querying is complete.
2οΈβ£ - Try to use Finally block as often possible.
3οΈβ£ - Release instances stored in Static Tables.
#java #memory #performance
@programmingTip
Measuring Time π°
Many applications require a very precise time measurement. For this purpose, Java provides static methods in System class. π
1οΈβ£ - currentTimeMillis(): Returns current time in MilliSeconds since Epoch Time, in Long.
view sourceprint?
2οΈβ£ - nanoTime(): Returns the current value of the most precise available system timer, in NanoSeconds, in long.
nanoTime() is meant for measuring relative time interval instead of providing absolute timing.
#java #time
@ProgrammingTip
Many applications require a very precise time measurement. For this purpose, Java provides static methods in System class. π
1οΈβ£ - currentTimeMillis(): Returns current time in MilliSeconds since Epoch Time, in Long.
view sourceprint?
long startTime = System.currentTimeMillis();
long estimatedTime = System.currentTimeMillis() - startTime;
2οΈβ£ - nanoTime(): Returns the current value of the most precise available system timer, in NanoSeconds, in long.
nanoTime() is meant for measuring relative time interval instead of providing absolute timing.
long startTime = System.nanoTime();
long estimatedTime = System.nanoTime() - startTime;
#java #time
@ProgrammingTip
πΌ Rescale Image In Java πΌ
An image can rescaled using AffineTransform. First of all, Image Buffer of input image is created and then scaled image is rendered. βοΈ
#java #image
@ProgrammingTip
An image can rescaled using AffineTransform. First of all, Image Buffer of input image is created and then scaled image is rendered. βοΈ
BufferedImage imgSource =
ImageIO.read(new File("myImage.jpg"));
BufferedImage imgDest =
new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = imgDest.createGraphics();
AffineTransform affinetransformation =
AffineTransform.getScaleInstance(2, 2);
g2d.drawRenderedImage(imgSource,
affinetransformation);
ImageIO.write(imgDest, "JPG",
new File("outImage.jpg"));
#java #image
@ProgrammingTip
π¦ Grab The Stack Trace Of An Exception π¦
Checking your code for errors is one of the most Painstaking β and frustrating β parts of the development process. ππ»ββοΈ
Itβs also horrendously Time-Consuming, particularly if youβve a pesky error whose source isnβt immediately clear. π‘
Using this code, you can more easily track down where an Exception is occurring β and bring yourself that much closer to being rid of it. β
Code
#java #exception #error #trick
@ProgrammingTip
Checking your code for errors is one of the most Painstaking β and frustrating β parts of the development process. ππ»ββοΈ
Itβs also horrendously Time-Consuming, particularly if youβve a pesky error whose source isnβt immediately clear. π‘
Using this code, you can more easily track down where an Exception is occurring β and bring yourself that much closer to being rid of it. β
Code
Exception e = β¦;
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
String trace = sw.getBuffer().toString();
#java #exception #error #trick
@ProgrammingTip
Use String.valueOf() instead of toString() π
If obj needs to be converted to string then the result of obj.toString() and String.valueOf(obj) will be same.
But String.valueOf() is Null Safe, means it will never throw NullPointerException. π―
γ°γ°γ°γ°γ°γ°
#java #string
@ProgrammingTip
If obj needs to be converted to string then the result of obj.toString() and String.valueOf(obj) will be same.
But String.valueOf() is Null Safe, means it will never throw NullPointerException. π―
Test test = null;
// Below statement will not throw NPE
System.out.println(String.valueOf(test));
// Next statement will throw NPE
System.out.println(test.toString())
γ°γ°γ°γ°γ°γ°
#java #string
@ProgrammingTip
πβ° What is Glide Library β°π
Glide is a fast and efficient Open Source Media Management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack. ππ»ββοΈ
By default Glide uses a custom HttpUrlConnection based stack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead. π
For learn more about Glide see
https://github.com/bumptech/glide π
#android #java #media
@ProgrammingTip
Glide is a fast and efficient Open Source Media Management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack. ππ»ββοΈ
By default Glide uses a custom HttpUrlConnection based stack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead. π
For learn more about Glide see
https://github.com/bumptech/glide π
#android #java #media
@ProgrammingTip
GitHub
GitHub - bumptech/glide: An image loading and caching library for Android focused on smooth scrolling
An image loading and caching library for Android focused on smooth scrolling - bumptech/glide
Google Gson Converter π₯
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. π¦
Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. π₯
There are a few open-source projects that can convert Java objects to JSON. π
However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. ππ»ββοΈ
For more about Gson see :
https://github.com/google/gson
#java #android #converter
@ProgrammingTip
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. π¦
Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. π₯
There are a few open-source projects that can convert Java objects to JSON. π
However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. ππ»ββοΈ
For more about Gson see :
https://github.com/google/gson
#java #android #converter
@ProgrammingTip
GitHub
GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back
A Java serialization/deserialization library to convert Java Objects into JSON and back - google/gson
Material Design The Development Documentation
This site has a single list of available Material Components and samples of their usage for developer. π
The v7 appcompat library is used in website - provide support of material design user interface implementations for older Android platforms. ππ»ββοΈ
Note: Design Library - provides various material design components which are not part of Android SDK.
http://www.materialdoc.com/
#java #android #material
@ProgrammingTip
This site has a single list of available Material Components and samples of their usage for developer. π
The v7 appcompat library is used in website - provide support of material design user interface implementations for older Android platforms. ππ»ββοΈ
Note: Design Library - provides various material design components which are not part of Android SDK.
http://www.materialdoc.com/
#java #android #material
@ProgrammingTip
π Retrofit Library π
Retrofit turns your REST API into a Java Interface π
Itβs an elegant solution for organizing API Calls in a project. The request method and relative URL are added with an Annotation , which makes code clean and simple.
With annotations, you can easily add a request body, manipulate the URL or headers and add query parameters. π₯
Adding a return type to a method will make it synchronous, while adding a Callback will allow it to finish asynchronously with success or failure. π°
For learn more about Retrofit see :
https://github.com/square/retrofit
http://square.github.io/retrofit/
#java #android #library
@ProgrammingTip
Retrofit turns your REST API into a Java Interface π
Itβs an elegant solution for organizing API Calls in a project. The request method and relative URL are added with an Annotation , which makes code clean and simple.
With annotations, you can easily add a request body, manipulate the URL or headers and add query parameters. π₯
Adding a return type to a method will make it synchronous, while adding a Callback will allow it to finish asynchronously with success or failure. π°
For learn more about Retrofit see :
https://github.com/square/retrofit
http://square.github.io/retrofit/
#java #android #library
@ProgrammingTip
GitHub
GitHub - square/retrofit: A type-safe HTTP client for Android and the JVM
A type-safe HTTP client for Android and the JVM. Contribute to square/retrofit development by creating an account on GitHub.
Listing Content Of Directory In Java π
In order to list the contents of a directory, below program can be used. π
This program simply receives the names of the all sub-directory and files in a folder in an Array and then that array is sequentially traversed to list all the contents. π
ββββββββββββ
#java #io
@ProgrammingTip
In order to list the contents of a directory, below program can be used. π
This program simply receives the names of the all sub-directory and files in a folder in an Array and then that array is sequentially traversed to list all the contents. π
import java.io.*;
public class ListContents {
public static void main(String[] args) {
File file =
new File("//home//user//Documents/");
String[] files = file.list();
System.out.println (
"Listing contents of " + file.getPath()
);
for(int i=0 ; i < files.length ; i++) {
System.out.println(files[i]);
}
}
}
ββββββββββββ
#java #io
@ProgrammingTip
Compare two string by equals() instead == in java π‘
Use equals() because this method internally checks == plus content equality check. βοΈ
[ CODE ]
[ RESULT ]
γ°γ°γ°γ°γ°γ°γ°γ°γ°γ°
#java
@PorgrammingTip
Use equals() because this method internally checks == plus content equality check. βοΈ
[ CODE ]
public class Test {
public static void main(String[] args) {
String s1 = "string";
String s2 = "string";
String s3 = new String("string");
String s4 = s3;
String s5 = "str"+"ing";
System.out.println("s1==s2 :"+(s1==s2));
System.out.println("s1==s3 :"+(s1==s3));
System.out.println(
"s1.equals(s3) :"+s1.equals(s3)
);
System.out.println("s3==s4 :"+(s3==s4));
System.out.println(
"s3.equals(s4) :"+s3.equals(s4)
);
System.out.println("s1==s5 :"+(s1==s5));
System.out.println(
"s1.equals(s5) :"+s1.equals(s5)
);
}
}
[ RESULT ]
s1==s2 :true
s1==s3 :false
s1.equals(s3) :true
s3==s4 :true
s3.equals(s4) :true
s1==s5 :true
s1.equals(s5) :true
γ°γ°γ°γ°γ°γ°γ°γ°γ°γ°
#java
@PorgrammingTip
Dagger 2
Dagger 2 is a dependency injection (DI) framework. It's based on the javax.inject annotations standard.
[GitHub] : http://bit.ly/2gY3BND
γ°γ°γ°γ°γ°γ°
#Dagger #Java #Android
@ProgrammingTip
Dagger 2 is a dependency injection (DI) framework. It's based on the javax.inject annotations standard.
[GitHub] : http://bit.ly/2gY3BND
γ°γ°γ°γ°γ°γ°
#Dagger #Java #Android
@ProgrammingTip
JUnit βοΈ
JUnit is a Unit Testing framework for the Java programming language. βοΈ
JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.
https://ibb.co/f0EFR5
[Website] : http://junit.org/junit5/
γ°γ°γ°γ°γ°
#java #junit
@ProgrammingTip
JUnit is a Unit Testing framework for the Java programming language. βοΈ
JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.
https://ibb.co/f0EFR5
[Website] : http://junit.org/junit5/
γ°γ°γ°γ°γ°
#java #junit
@ProgrammingTip
imgbb.com
junit5 banner
Image junit5 banner hosted in imgbb.com
This media is not supported in your browser
VIEW IN TELEGRAM
Codota : AI Pair Programmer π€
Codota understands the world's code and provides you with the right suggestion at the right time.
Supports : Java β¨οΈ
www.codota.com
γ°γ°γ°γ°γ°γ°
#Java #Tools
@ProgrammingTip
Codota understands the world's code and provides you with the right suggestion at the right time.
Supports : Java β¨οΈ
www.codota.com
γ°γ°γ°γ°γ°γ°
#Java #Tools
@ProgrammingTip
Lambda in Java 8 π₯
Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. 8οΈβ£
Lambda expression facilitates functional programming, and simplifies the development a lot. π€·π»ββοΈ
γ°γ°γ°γ°γ°γ°
#java #lambda
@ProgrammingTip
https://t.me/pgimg/6
Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. 8οΈβ£
Lambda expression facilitates functional programming, and simplifies the development a lot. π€·π»ββοΈ
γ°γ°γ°γ°γ°γ°
#java #lambda
@ProgrammingTip
https://t.me/pgimg/6
Telegram
Programming Tips Resources
Avian π
A lightweight virtual machine and class library designed to provide a useful subset of Javaβs features, suitable for building self-contained applications. π‘
Efficient
πΈJust-In-Time (JIT) compilation for fast method execution
πΉGenerational, copying garbage collection ensures short pause times and good spatial locality
πΈThread-local heaps provide O(1) memory allocation with no synchronization overhead
πΉNull pointer dereferences are handled via OS signals to avoid unecessary branches
The class library is designed to be as loosely coupled as possible, allowing tools like ProGuard to aggressively isolate the minimum code needed for an application. β‘οΈ
This translates to smaller downloads and faster startup. β¨
https://t.me/pgimg/135
[ Website ] : readytalk.github.io/avian
γ°γ°γ°γ°γ°γ°
#Java #VM #JIT
@ProgrammingTip
A lightweight virtual machine and class library designed to provide a useful subset of Javaβs features, suitable for building self-contained applications. π‘
The VM is implemented from scratch and designed to be both fast and small.
Efficient
πΈJust-In-Time (JIT) compilation for fast method execution
πΉGenerational, copying garbage collection ensures short pause times and good spatial locality
πΈThread-local heaps provide O(1) memory allocation with no synchronization overhead
πΉNull pointer dereferences are handled via OS signals to avoid unecessary branches
The class library is designed to be as loosely coupled as possible, allowing tools like ProGuard to aggressively isolate the minimum code needed for an application. β‘οΈ
This translates to smaller downloads and faster startup. β¨
https://t.me/pgimg/135
[ Website ] : readytalk.github.io/avian
γ°γ°γ°γ°γ°γ°
#Java #VM #JIT
@ProgrammingTip
Telegram
Programming Tips Resources
βSorry JAVA, I am done with you. I have fallen in love with Kotlin.β
β- Sad Love Story πGoogle started supporting Kotlin as an official language for Android development. β
On that day I decided, If Google somehow exclude JAVA then I am gonna leave Android development to continue my relation with JAVA. β
I was very egoistic that no any other language can took place in my heart. π
One day, One of my friends suggest me to give at least one chance to Kotlin. Because JAVA does not value my relationship. π π»ββοΈ
Since we have been together from last 6 years sometimes I got angry and scolding on JAVA that It can not do some simple things by itself. π€·π»ββοΈ
Every time I need to write boilerplate code again and again, but still I was appreciating JAVA in front of everyone. π
[ Article ] : kutt.it/ktjv
γ°οΈγ°οΈγ°οΈγ°οΈγ°οΈγ°οΈ
#Kotlin #Android #Java
@ProgrammingTip
Telegram
Programming Tips Resources