Programming Tips πŸ’‘
51.5K 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
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
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
Avian πŸ—‚

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
β€œ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