Programming Tips πŸ’‘
51.6K subscribers
67 photos
10 videos
30 files
354 links
Programming & AI:
Tips πŸ’‘
Articles πŸ“•
Resources πŸ‘Ύ
Design Patterns πŸ’Ž
Software Principles βœ…

πŸ‡³πŸ‡± Contact: @MoienTajik

🎯 Buy ads: https://telega.io/c/ProgrammingTip
Download Telegram
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:

//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
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?

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. βœ”οΈ

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
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. 🎯

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
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
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
πŸ’š 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
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. πŸ’Ž

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 ]
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
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